Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Arrow 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.

    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!).
    Last edited by SPACE MONKEY; February 27th, 2011 at 10:38 PM.


  2. #2
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Rounding long or floating numbers, or longs for that matter.

    Dear All:

    Figured it out.

     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.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

  4. The Following User Says Thank You to javapenguin For This Useful Post:

    SPACE MONKEY (February 28th, 2011)

  5. #4
    Member
    Join Date
    Feb 2011
    Location
    Pittsburgh
    Posts
    62
    My Mood
    Angelic
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. using Eclipse and every program gives the errorr with floating numbers
    By Neera in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2010, 12:11 PM
  2. Rounding an int. Help please
    By Akim827 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2010, 12:36 AM
  3. [SOLVED] Why is my float automatically rounding and how do i get it to stop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 18
    Last Post: August 26th, 2010, 01:41 PM
  4. How long did it take you to learn?
    By JavaLearner in forum The Cafe
    Replies: 5
    Last Post: March 24th, 2010, 04:42 PM
  5. Rounding screwed up?
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2010, 01:43 PM