2

Possible Duplicate:
Is there an equivalent for toPrecision() in Java?

I created a method which suppose to limit the number to only 2 decimal places but what it does it shows numbers to 2 decimal places and then display zeros after that.

Also, from the number computation below I can notice that it rounds the numbers incorrectly. The numbers should be 478.03 129.06 348.97.

What's wrong here?

PS. I'm just following pseudocode and I can't import anything more than import java.io.*;

My output:

Employee's Last Name: dfsdfsdf
Employment Status (F or P): p
Hourly Pay Rate: 8.35
Hours Worked: 51.5
-------------------------------------------------------
Name    Status      Gross   Taxes   Net
dfsdfsdf    Part Time       478.040000  129.070000  348.970000

My code where I input all data, and then attempting to output it:

private static void outputData(String name, char status, double pay) 
{
    if (status == "p".charAt(0))
    {
        System.out.println("-------------------------------------------------------");
        System.out.println("Name\tStatus\t\tGross\tTaxes\tNet");
        System.out.printf("%s\tPart Time\t\t%f\t%f\t%f\n\n", 
                name, 
                roundDouble(pay), 
                roundDouble(calculateTaxes(pay)), 
                roundDouble(pay - calculateTaxes(pay)));
    }
    else if (status == "f".charAt(0))
    {
        System.out.println("-------------------------------------------------------");
    }
}

More code which is the method that should do conversion:

private static double roundDouble(double number) 
{
    return Double.parseDouble(String.format("%.2f", number));
}
Community
  • 1
  • 1
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
  • Just a small throw in: You might want to check something like Guava for more robust rounding semantics: http://code.google.com/p/guava-libraries/wiki/MathExplained#Floating-point_arithmetic – Stephan Sep 21 '12 at 06:54
  • Also, let's say that I can't include anything more than `import java.io.*;`. – HelpNeeder Sep 21 '12 at 06:55
  • 1
    Not part of `java.io` but part of Java, you might want to represent currency as [`BigDecimal`](http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html) instead of doubles (because floating point operations inherently add errors to the calculation). – Stephan Sep 21 '12 at 06:59
  • Possibly you can try referring this question http://stackoverflow.com/questions/50532/how-do-i-format-a-number-in-java – Karthikeyan Arumugam Sep 21 '12 at 07:06

2 Answers2

3

You could try NumberFormat instead

double value = 478.03123456789;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);

System.out.println(nf.format(value));

Outputs 478.03

ps - It might help with had the original values as well ;)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

try to do it like this:

private static double roundDouble(double number) 
{
    return java.lang.Math.round((number * 100) / 100.0));
}
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • This returns: `478.000000 129.000000 349.000000`. – HelpNeeder Sep 21 '12 at 07:04
  • Did you try it using 100.0 or after you changed my answer? – CloudyMarble Sep 21 '12 at 07:25
  • With this code: `return java.lang.Math.round(number * 100) / 100.00;` I get `478.04 129.07 348.97`. I got rid of trailing 0's other way though. – HelpNeeder Sep 21 '12 at 07:28
  • I thought its what you need! how do you want to have your output? whats the number giver to the function which should give 478.03 back? – CloudyMarble Sep 21 '12 at 07:34
  • Ok, the problem with my code was that print formatting with %f returned all 0's. So I just used print with out formatting and added %.2f. :) And yes, now I can't figure out why it display 478.04 instead of 478.03. =/ I'm googling this right now. – HelpNeeder Sep 21 '12 at 07:37
  • This works: `return Double.parseDouble(String.valueOf((int)(number * 100))) / 100;`. Output: `478.03 129.06 348.97` So thanks :D – HelpNeeder Sep 21 '12 at 07:49
  • you welcome, didnt expect it to work with decimal 100 – CloudyMarble Sep 21 '12 at 07:54
  • I think that the problem was that double already rounds the numbers. Using integer value instead, eliminated this rounding, somehow. – HelpNeeder Sep 21 '12 at 07:58