Rounding long or floating numbers, or longs for that matter.

• February 27th, 2011, 09:35 PM
SPACE MONKEY
Rounding long or floating numbers, or longs for that matter.
Dear All:

Simple code to ask you your wages, get the tax rate, and give you the value of the tax owed first the dollars, as an int type, and then display the cents owed as a double or long.

What I can not work out how to do is set the significant number to two digits after the ".".

I have looked at the AIP and found "round.(a,2)" but I do not seem to get this to work.
The error I get is "can not find symbol".
symbol : method round(java.lang.Double,int);
location: class TaxBill
long ttaxDue = round(calcTaxDue, 2);
^

The code is as follows.

Code Java:

```import java.util.Scanner; import static java.lang.Math.*; import java.lang.Math.*; import java.lang.Double; import java.lang.Enum.*; public class TaxBill { //public static void final RoundingMode HALF_UP; //This has errors but not what I want to concentrate on. public static void main(String[] args) { //Initalize the variable "ssalary" and get the input from the user. float ssalary = 00.00f; System.out.print("What is your current salary (in USD)?: \$"); Scanner keyboard = new Scanner(System.in); ssalary = keyboard.nextFloat();   double ttaxRate = 0.00; System.out.print("What is your current tax rate?: %"); ttaxRate = keyboard.nextDouble();   Double calcTaxDue = (ssalary / 100) * ttaxRate;   long ttaxDue = round(calcTaxDue, 2); //ROUND ROUND ROUND ROUND ROUND ROUND ROUND   int ttaxDollars = (int) Math.floor(calcTaxDue); int ttaxCents = (int) ttaxDue - ttaxDollars; // so I need the ttaxDue value rounded so this calculation gives me the cents left, and I want to know how to round stuff.   System.out.println("You made \$" + ssalary); System.out.println("You paid in tax: \$" + ttaxDollars); System.out.println("You paid in tax: c" + ttaxCents);```

Thanks for any help.

SPACE MONKEY, (I buy space bananas!).
• February 27th, 2011, 11:11 PM
SPACE MONKEY
Re: Rounding long or floating numbers, or longs for that matter.
Dear All:

Figured it out.

Code Java:

``` import java.util.Scanner; import static java.lang.Math.*; import java.lang.Math.*; import java.lang.Double; import java.lang.Enum.*; public class TaxBill { //public static void final RoundingMode HALF_UP; //This has errors but not what I want to concentrate on. public static void main(String[] args) { //Initalize the variable "ssalary" and get the input from the user. float ssalary = 00.00f; System.out.print("What is your current salary (in USD)?: \$"); Scanner keyboard = new Scanner(System.in); ssalary = keyboard.nextFloat();   double ttaxRate = 0.00; System.out.print("What is your current tax rate?: %"); ttaxRate = keyboard.nextDouble();   Double calcTaxDue = (ssalary / 100) * ttaxRate; //this number is like 23.2333434343 in terms of digits after the ".". System.out.println(calcTaxDue); //So you can see if you get what you expect. //*int allCents = round(calcTaxDue * 100);*/ //Reccommended way. int dollars = (int) Math.floor(calcTaxDue); //SPACE MONKEY WAY //int dollars = allCents / 100; //Recommended way int cents = (int) (round(calcTaxDue * 100) % 100);//SPACE MONKEY WAY //int cents = allCents % 100; //Recommended way     //int ttaxDollars = (int) Math.floor(calcTaxDue);   //int ttaxCents = (int) ttaxDue - ttaxDollars;   System.out.println("You made \$" + ssalary); //System.out.println("You paid in tax: \$" + ttaxDollars);   System.out.println("The amount of tax dollars you must pay is: \$" + dollars); System.out.println("The amount of tax cents you must pay is : c" + cents); }}```

why is there no rounding command. I can not find it. I think the round is not what I had expected and I do not understand what the AIP is telling me, to me it looked like round was, round(this value, to this number of significant digits). So go figure.

Cheers.
• February 27th, 2011, 11:13 PM
javapenguin
Re: Rounding long or floating numbers, or longs for that matter.
Well, you could go here:
Using Predefined Formats (The Java™ Tutorials > Internationalization > Formatting)

Or here:

DecimalFormat (Java 2 Platform SE v1.4.2)

Or
System.out.printf('%.2f', float f ) : System.out.printfjava.langJava by API

I'm not very good at the System.out.printf() method but that's probably what you're looking for.
• February 27th, 2011, 11:32 PM
SPACE MONKEY
Re: Rounding long or floating numbers, or longs for that matter.
Dear JP:

First link very helpful, second link I had and got nowhere tying to see how that page helped me round stuff to two significant places before I posted for an answer.

I would have thought that there must be a set all Doubles to two significant numbers after the ".".

But I have heard about the Currency class. and I guess it is in there somewhere.

The third post is/was odd and no idea what is going on there.

Thanks again I will read those three through again tomorrow morning. Oh coffee!!

SPACE MONKEY