Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-01-2008, 11:00 AM
Member
 
Join Date: Feb 2008
Posts: 8
JavaInLove is on a distinguished road
Java Algorithm problems?
Right, since im a newbie in java please be nice

Look at following 2 statements in java code:
Code:
System.out.println(456.8 + 0.09); //456.89 (WORKS as expected) System.out.println(23.4 + 0.09); //23.49 (WORKS as expected) System.out.println(1.8 + 0.09); //1.8900.....01 (DOESNT WORK as expected)
i've been told that the above situation happens because of floating points and computer calculations and not an error.

So what i want to discuss is that how do i rectify it? And what is the best way to solve it so that i want 1.89 to be displayed instead of ...

Im writing a calculator so i need 1.89 to be displayed on screen rather than 1.8900..01. Currently those display values are held in a variable of double.
DO i change it to float or something else so that it can accept a bigger range?

Any help would be greatful! Thanks in advance...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-01-2008, 11:30 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
In default if you use a decimal number VM take it as a double value. If you define them as a float value, you get the correct value.

And see the number of decimal you get, in your code.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-01-2008, 11:55 AM
Member
 
Join Date: Feb 2008
Posts: 8
JavaInLove is on a distinguished road
Whats VM?????
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-01-2008, 12:05 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
VM - Virtual Machine (java virtual machine normally call like that)
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-01-2008, 05:19 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,618
Norm is on a distinguished road
There are several ways to format the values of float and doubles for display using java classes. For example DecimalFormat

If you read your text you should see that double has a larger range than float. But your problem is displaying the value, so you need to format it to what you want to see.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-02-2008, 09:24 AM
Alan-LB's Avatar
Member
 
Join Date: Jun 2008
Location: Junee, NSW, Australia
Posts: 19
Alan-LB is on a distinguished road
Send a message via Yahoo to Alan-LB
You can use System.out.printf to format numbers. This would leave off the last few decimal positions.

System.out.printf("%10.5f\n", 1.8 + .09);

java(dot)sun(dot)com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.lang.String,%20java.l ang.Object...)

Alan.
__________________
There are 10 types of people - those who understand binary and those who don't!!
Today is the Beta version of Tomorrow!!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-02-2008, 09:30 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Hey Alan, can't you add links in such situations. It's better to add a link to the URL when you posting links.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-02-2008, 09:44 AM
Alan-LB's Avatar
Member
 
Join Date: Jun 2008
Location: Junee, NSW, Australia
Posts: 19
Alan-LB is on a distinguished road
Send a message via Yahoo to Alan-LB
As long as the OP can find his way to the reference I wanted him to see.

I am told I can't post links or images - so I posted the link my way!!!

Alan.
__________________
There are 10 types of people - those who understand binary and those who don't!!
Today is the Beta version of Tomorrow!!
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-02-2008, 09:50 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,581
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Oki, don't worry in near future we may be fix that issue. You know that, at the time we are discussing this.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Algorithm statement in my Java codes javanewbie New To Java 2 06-24-2008 03:52 PM
Soundex Algorithm Implementation in Java Java Tip java.lang 0 04-12-2008 09:40 PM
Using Java To Implement RSA Algorithm Floetic New To Java 3 04-01-2008 12:56 AM
textfile with Java problems saytri New To Java 4 12-29-2007 06:16 PM
Help with algorithm in java coco AWT / Swing 1 08-01-2007 07:45 AM


All times are GMT +3. The time now is 08:03 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org