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

Username:   Password: 
 RegisterRegister   
 How to make FLOAT add upto 2 decimal places (56.90)
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
w0lv3rin3




PostPosted: Sat Sep 13, 2003 8:55 pm   Post subject: 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?

code:

                // 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
Sponsor
Sponsor
Sponsor
sponsor
Catalyst




PostPosted: Sun Sep 14, 2003 12:34 am   Post subject: (No subject)

multiply it by 100 then cast it to an int then divide by 100 then cast back to float
w0lv3rin3




PostPosted: Sun Sep 14, 2003 10:00 am   Post subject: (No subject)

multipy this

code:
subTotal = gum + lolly + sugar;


then divide this?

code:
total = subTotal + tax;
Catalyst




PostPosted: Sun Sep 14, 2003 2:15 pm   Post subject: (No subject)

whatever u would like to round do this (lets say the variable is named x)

code:
x=(float)((int)x*100)/100;
w0lv3rin3




PostPosted: Sun Sep 14, 2003 2:41 pm   Post subject: (No subject)

I got yah now! thx alot dude.

i just did:

code:

        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




PostPosted: Sun Sep 14, 2003 2:58 pm   Post subject: (No subject)

i think i figured it out:

i could just do this:

code:
        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




PostPosted: Sun Sep 14, 2003 3:01 pm   Post subject: (No subject)

if its rong could u put another example if u dont mind
Catalyst




PostPosted: Sun Sep 14, 2003 3:53 pm   Post subject: (No subject)

what do u mean round up one?
w0lv3rin3




PostPosted: Sun Sep 14, 2003 7:44 pm   Post subject: (No subject)

like
56.78
round it up to 56.79
Dan




PostPosted: Sun Sep 14, 2003 10:00 pm   Post subject: (No subject)

w0lv3rin3 wrote:
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
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
krishon




PostPosted: Mon Sep 15, 2003 6:25 pm   Post subject: (No subject)

lol
krishon




PostPosted: Thu Sep 25, 2003 8:06 pm   Post subject: (No subject)

well, i would say to use casting, just like catalyst said, its the nice ez way to change things....java really does rock Very Happy
Tony




PostPosted: Thu Sep 25, 2003 8:20 pm   Post subject: (No subject)

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.

code:

System.out.println((int)(num*100)/100.0);


Notice the .0 Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
rizzix




PostPosted: Thu Sep 25, 2003 11:04 pm   Post subject: (No subject)

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:
code:

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
code:

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




PostPosted: Thu Sep 25, 2003 11:17 pm   Post subject: (No subject)

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.
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 1  [ 15 Posts ]
Jump to:   


Style:  
Search: