Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / December 2007

Tip: Looking for answers? Try searching our database.

what is wrong with float?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sasi - 09 Dec 2007 07:34 GMT
create a J# windows applicaion.add a button , a label and two textboxes to
form and paste this code into the buttons event handler:

private void button1_Click(Object sender, System.EventArgs e)
    {
        label1.set_Text(Float.toString(
            Float.parseFloat(textBox1.get_Text())
            *
            Float.parseFloat(textBox2.get_Text())
            ));
    }

when you run the project and test it with 3 and 6.7 ; you get freaking
result of "20.0999985" in the label instead of "20.1" .if try 3 and
6.71,you'll get "20.1300011" instead of "20.13" .

what the hell is wrong with J# float numbers?
Phill W. - 11 Dec 2007 12:59 GMT
> when you run the project and test it with 3 and 6.7 ; you get freaking
> result of "20.0999985" in the label instead of "20.1" .if try 3 and
> 6.71,you'll get "20.1300011" instead of "20.13" .
>
> what the hell is wrong with J# float numbers?

The same thing that's wrong with floating point numbers in just about
/every/ computing language - they are only /approximations/ of the
intended value.

You have to deal with this vagueness, usually by formatting the result
to the required degree of accuracy:

label1.set_Text(
   ( Float.parseFloat( textBox1.get_Text() )
     *
     Float.parseFloat( textBox2.get_Text() )
   ).toString( "0.00" );

(Or something fairly close to this - I've never used J#)   :-)

HTH,
   Phill  W.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.