Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Quick question on rounding :-)
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xHoly-Divinity




PostPosted: Mon Oct 03, 2005 3:33 pm   Post subject: Quick question on rounding :-)

Quick question. How do you round to two decimal places. I need two decimal places because it is dealing with money. Thanks
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Oct 03, 2005 3:43 pm   Post subject: (No subject)

rounding money? Go rent Office Space Laughing

Work in whole units (cents), the rest is just formatting.
xHoly-Divinity




PostPosted: Mon Oct 03, 2005 3:47 pm   Post subject: (No subject)

There is no way just to round to two decimals??
Tony




PostPosted: Mon Oct 03, 2005 3:55 pm   Post subject: (No subject)

multiply by 100, round, then divide back by 100
wtd




PostPosted: Mon Oct 03, 2005 3:57 pm   Post subject: (No subject)

Don't keep track of money using floating point numbers.
Hikaru79




PostPosted: Mon Oct 03, 2005 4:54 pm   Post subject: (No subject)

wtd wrote:
Don't keep track of money using floating point numbers.

Amen. If you don't know why he's saying that, just run this:
Java:
        public static void main(String [] args){
                double x = 5.02;
                double y = 0.01;
                System.out.println(x+y);
        }

It's just ridiculous that an enterprise-level language like Java can't do something as simple as add. wtd, if there's a technical reason behind this, could you explain it, 'cause I was just stunned when I first discovered this.
1of42




PostPosted: Mon Oct 03, 2005 5:19 pm   Post subject: (No subject)

Java:

DecimalFormat formatter = new DecimalFormat ("$0.00");
System.out.println(formatter.format(someNumber));


This will print out a formatted number for money - you don't need to round beforehand, it will take care of that stuff for you.
rizzix




PostPosted: Tue Oct 04, 2005 10:09 am   Post subject: (No subject)

even better

Java:
System.out.printf("%.2f", someNumber);
Or if you are going to save it as a string...

Java:
String s = String.format("%.2f", someNumber);



Hikaru79: Java float's will behave the way you're expecting them to behave (for small numbers).. the doubles always behave likewise.. You have to round them to correct decimal place... Also.. if you need arbitrary precission.. then there's always the BigDecimal and BigInteger class.

Besides java's floating points strictly follow the IEEE 754 specs.. i guess that's just the way the arithmetic is ...according to those specs.
Sponsor
Sponsor
Sponsor
sponsor
beard0




PostPosted: Tue Oct 04, 2005 11:57 am   Post subject: (No subject)

Hikaru79 wrote:
wtd wrote:
Don't keep track of money using floating point numbers.

Amen. If you don't know why he's saying that, just run this:
Java:
        public static void main(String [] args){
                double x = 5.02;
                double y = 0.01;
                System.out.println(x+y);
        }

It's just ridiculous that an enterprise-level language like Java can't do something as simple as add. wtd, if there's a technical reason behind this, could you explain it, 'cause I was just stunned when I first discovered this.


Check this one out:
Java:
public static void main(String [] args){
                double x = 0.02;
                double y = 0.01;
                double z = 5;
                if (z+x+y == x+y+z){
                  System.out.println("Good");
                }else{
                  System.out.println("What the hell?!");
                };
}
wtd




PostPosted: Tue Oct 04, 2005 12:06 pm   Post subject: (No subject)

code:
irb(main):001:0> x, y, z = 0.02, 0.01, 5.0
=> [0.02, 0.01, 5.0]
irb(main):002:0> z + x + y
=> 5.03
irb(main):003:0> x + y + z
=> 5.03
irb(main):004:0> z + x + y == x + y + z
=> false
irb(main):005:0>
beard0




PostPosted: Tue Oct 04, 2005 12:24 pm   Post subject: (No subject)

wtd wrote:
code:
irb(main):001:0> x, y, z = 0.02, 0.01, 5.0
=> [0.02, 0.01, 5.0]
irb(main):002:0> z + x + y
=> 5.03
irb(main):003:0> x + y + z
=> 5.03
irb(main):004:0> z + x + y == x + y + z
=> false
irb(main):005:0>


What is this?
Tony




PostPosted: Tue Oct 04, 2005 12:39 pm   Post subject: (No subject)

beard0 wrote:
What is this?

That was Ruby. wtd was showing you that it's the same across different languages.
Turing:

var x, y, z : real
x := 0.02
y := 0.01
z := 5.0

put x + y + z
put z + y + x
put x + y + z = z + y + x

Turing too.
beard0




PostPosted: Tue Oct 04, 2005 1:07 pm   Post subject: (No subject)

Hmm, looks like Java is in some ways the best of the bunch then, as it actually outputs two different numbers, rather than outputing 5.03 both times, and then claiming that they are not equal.

Damn, php too.
PHP:
<?php
$x = 0.2;
$y = 0.01;
$z = 5.0;

echo $x + $y + $z . "\n";
echo $z + $y + $x . "\n";
if ($x + $y + $z == $z + $y + $x){
        echo "True!\n";
}else{
        echo "False\n";
}
if ($x + $y + $z === $z + $y + $x){
        echo "True!\n";
}else{
        echo "False\n";
}
?>


Is this then a hardware issue? I could see it comming from decimal terminating numbers being non-terminators in binary, if real numbers/doubles are held in memory in something like scientific notation, so that having the 5 added at the begining allows for less precision after the decimal place.
Tony




PostPosted: Tue Oct 04, 2005 2:09 pm   Post subject: (No subject)

beard0 wrote:

Is this then a hardware issue?

Not quite, as rizzix pointed out
rizzix wrote:

java's floating points strictly follow the IEEE 754 specs

IEEE 754 wrote:

Why is 0.1 not 0.1?

Binary floating-point numbers consist of signed integers multiplied by powers of two. When fractional, you can also consider them as integers divided by powers of two. The decimal number 0.1, or 1/10, is not an integer over a power of two. The 854 standard encompasses decimal arithmetic, but there is little hardware support outside of desktop calculators.


Turing:

put 5 + 0.1 + 0.01 = 0.01 + 0.01 + 5 %false
put 10 + 0.1 + 0.01 = 0.01 + 0.1 + 10 %true
rizzix




PostPosted: Tue Oct 04, 2005 2:15 pm   Post subject: (No subject)

No not all languages... Languages like Haskell should not have such a problem.. In haskell the floating points are represented differently.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 27 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: