Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Floating point ambiguous errors

Floating point ambiguous errors

Scheduled Pinned Locked Moved Visual Basic
questionhelptutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Codemonkey85
    wrote on last edited by
    #1

    Okay, here's the deal. Let's just say I have a variable, amtChange, which is a single floating point number. Let's say that it's 1.23 in this example. If I subtract amtDol, which is an integer with a value of 1, I end up with .2299995 as a difference. I find that this happens often. How can I prevent and/or fix this? Oh, here's my code: Private amtOwed, amtPaid, amtChange As Single Private amtDol, amtQuar, amtDime, amtNick, amtPen As Integer *** amtOwed = Val(txtOwed.Text) amtPaid = Val(txtPaid.Text) amtChange = amtPaid - amtOwed amtDol = Int(amtChange) amtChange = amtChange - amtDol MsgBox(amtChange) And the Message Box gives me .2299995!

    L T 2 Replies Last reply
    0
    • C Codemonkey85

      Okay, here's the deal. Let's just say I have a variable, amtChange, which is a single floating point number. Let's say that it's 1.23 in this example. If I subtract amtDol, which is an integer with a value of 1, I end up with .2299995 as a difference. I find that this happens often. How can I prevent and/or fix this? Oh, here's my code: Private amtOwed, amtPaid, amtChange As Single Private amtDol, amtQuar, amtDime, amtNick, amtPen As Integer *** amtOwed = Val(txtOwed.Text) amtPaid = Val(txtPaid.Text) amtChange = amtPaid - amtOwed amtDol = Int(amtChange) amtChange = amtChange - amtDol MsgBox(amtChange) And the Message Box gives me .2299995!

      L Offline
      L Offline
      LokmanHakim
      wrote on last edited by
      #2

      i'm not expert in this but try to change the data type into double :)

      1 Reply Last reply
      0
      • C Codemonkey85

        Okay, here's the deal. Let's just say I have a variable, amtChange, which is a single floating point number. Let's say that it's 1.23 in this example. If I subtract amtDol, which is an integer with a value of 1, I end up with .2299995 as a difference. I find that this happens often. How can I prevent and/or fix this? Oh, here's my code: Private amtOwed, amtPaid, amtChange As Single Private amtDol, amtQuar, amtDime, amtNick, amtPen As Integer *** amtOwed = Val(txtOwed.Text) amtPaid = Val(txtPaid.Text) amtChange = amtPaid - amtOwed amtDol = Int(amtChange) amtChange = amtChange - amtDol MsgBox(amtChange) And the Message Box gives me .2299995!

        T Offline
        T Offline
        Tasnim
        wrote on last edited by
        #3

        Hi, You can try this. Define all the variables as double or type cast it into double and then subtract it. Take the result into a variable which is double. Tasnim

        C 1 Reply Last reply
        0
        • T Tasnim

          Hi, You can try this. Define all the variables as double or type cast it into double and then subtract it. Take the result into a variable which is double. Tasnim

          C Offline
          C Offline
          Codemonkey85
          wrote on last edited by
          #4

          As a matter of fact, it turns out the Decimal variable type doesn't generate ambiguous errors. Thanks for the input, though. I really appreciate it! :D

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups