
-----------------------------------
w0lv3rin3
Sat Sep 13, 2003 8:55 pm

How to make FLOAT add upto 2 decimal places (56.90)
-----------------------------------
When I program my program and run it I get:

Cheap Candy Store 
 
Stick of Gum 	 	10.25
Lolly Pop 	 	 	30.56
6 lbs of Sugar 	 	1.05
 
sub-total 	 	 	41.859997
TOTAL 	 	 	56.859997

now u see how it wont round. I would like it to round to
sub-total 	 	 	41.85
TOTAL 	 	 	56.85

any1 have any ideas?


		// declaration of a constant
		final float GST = 7.00f; // variable cannot be changed during the execution of this program
		final float PST = 8.00f; // variable cannot be changed during the execution of this program

		// declaration of variables
		float gum;
		float lolly;
		float sugar;
		float subTotal;
		float tax;
		float total;

		// assign values to the goods purchased
		tax = GST + PST; // GST and PST will be added up to become 15%
		// price list
		gum = 10.25f; // will make price of gum without tax
		lolly = 30.56f; // will make price of lollypop without tax
		sugar = 1.05f; // will make price of sugar without tax

		// calculate sub total of goods
		subTotal = gum + lolly + sugar; // adding up gum, lolly, sugar; stores the added data to subTotal

		// calculate total cost
		total = subTotal + tax; // adds the contents of subTotal and tax, then stores the data in total


-----------------------------------
Catalyst
Sun Sep 14, 2003 12:34 am


-----------------------------------
multiply it by 100 then cast it to an int then divide by 100 then cast back to float

-----------------------------------
w0lv3rin3
Sun Sep 14, 2003 10:00 am


-----------------------------------
multipy this

      subTotal = gum + lolly + sugar;

then divide this?

      total = subTotal + tax;

-----------------------------------
Catalyst
Sun Sep 14, 2003 2:15 pm


-----------------------------------
whatever u would like to round do this (lets say the variable is named x)

x=(float)((int)x*100)/100;

-----------------------------------
w0lv3rin3
Sun Sep 14, 2003 2:41 pm


-----------------------------------
I got yah now!  thx alot dude.

i just did:


        screen.write("sub-total \t \t \t" +(float)((int)(subTotal * 100)) / 100+ "\n");
        screen.write("TOTAL \t \t \t \t" +(float)((int)(total * 100)) / 100+ "\n");


just one more question, how would u round it up one

-----------------------------------
w0lv3rin3
Sun Sep 14, 2003 2:58 pm


-----------------------------------
i think i figured it out:

i could just do this:

        screen.write("sub-total \t \t \t" +(float)((int)(subTotal * 100 + 1)) / 100+ "\n");
        screen.write("TOTAL \t \t \t \t" +(float)((int)(total * 100 + 1)) / 100+ "\n");

this is correct right?

to round up one?

-----------------------------------
w0lv3rin3
Sun Sep 14, 2003 3:01 pm


-----------------------------------
if its rong could u put another example if u dont mind

-----------------------------------
Catalyst
Sun Sep 14, 2003 3:53 pm


-----------------------------------
what do u mean round up one?

-----------------------------------
w0lv3rin3
Sun Sep 14, 2003 7:44 pm


-----------------------------------
like
56.78
round it up to 56.79

-----------------------------------
Dan
Sun Sep 14, 2003 10:00 pm


-----------------------------------
like
56.78
round it up to 56.79

56.78 dont round up to 56.79, it whould go up to 56.8

or 56.785 whould go to 56.79

-----------------------------------
krishon
Mon Sep 15, 2003 6:25 pm


-----------------------------------
lol

-----------------------------------
krishon
Thu Sep 25, 2003 8:06 pm


-----------------------------------
well, i would say to use casting, just like catalyst said, its the nice ez way to change things....java really does rock :D

-----------------------------------
Tony
Thu Sep 25, 2003 8:20 pm


-----------------------------------
yup, casting is the way to go. But you dont need to recast it back to float, Java does it itself when you divide it by a float value.


System.out.println((int)(num*100)/100.0);


Notice the .0  :wink:

-----------------------------------
rizzix
Thu Sep 25, 2003 11:04 pm


-----------------------------------
well tony is right. But here java technically does not "cast" the result, but promotes all the lower types in the expression to the greatest type, thus the result is that of the greatest type in the expression.

what this means is it does not do an "implicit cast" like in some languages. 

to make things clear look at this expression:

2 + 5L + 3.0f + 1.0


The greatest type in this expression is double (which is the '1.0' literal).

Now in Java this is what happens: type promotion

2.0 + 5.0 + 3.0 + 1.0


as you can see everything in that expression is promoted to the greatest type, in this case double.

in other languages usually an implicit cast is done on the result of the expression, which tends to cause problems (in terms of accuracy) at times.

-----------------------------------
rizzix
Thu Sep 25, 2003 11:17 pm


-----------------------------------
one more thing, hope ur not confused on what an expression is...

look at this code:
(((2 * 3.0f) + 9L) - 1)

2*3f is an expression which returns a float, cuz of type promtion.

then the float is added to a long... result float!

then an int is subtracted from that float ... result float! 

keep in mind every num is type promoted.

so let's say we need a result of long then we could do this...
(( (long)(2 * 3.0f) + 9L) - 1)

here 2 * 3.0f will become 2.0f + 3.0f but then casted to long.

so long + 9L results to long ... no change.

long - 1L (because of type promotion 1 is now 1L) 

hence the result is long.


hope it's all clear now.
