Double value with specific precision in java

70,429

Solution 1

DecimalFormat will allow you to define how many digits you want to display. A '0' will force an output of digits even if the value is zero, whereas a '#' will omit zeros.

System.out.print(new DecimalFormat("#0.0000").format(value)+" kg\n"); should to the trick.

See the documentation

Note: if used frequently, for performance reasons you should instantiate the formatter only once and store the reference: final DecimalFormat df = new DecimalFormat("#0.0000");. Then use df.format(value).

Solution 2

add this instance of DecimalFormat to the top of your method:

DecimalFormat four = new DecimalFormat("#0.0000"); // will round and display the number to four decimal places. No more, no less.

// the four zeros after the decimal point above specify how many decimal places to be accurate to.
// the zero to the left of the decimal place above makes it so that numbers that start with "0." will display "0.____" vs just ".____" If you don't want the "0.", replace that 0 to the left of the decimal point with "#"

then, call the instance "four" and pass your double value when displaying:

double value = 0;
System.out.print(four.format(value) + " kg/n"); // displays 0.0000

Solution 3

System.out.format("%.4f kg\n", 0.0d) prints '0.0000 kg'

Solution 4

I suggest you to use the BigDecimal class for calculating with floating point values. You will be able to control the precision of the floating point arithmetic. But back to the topic :)

You could use the following:

static void test(String stringVal) {
    final BigDecimal value = new BigDecimal(stringVal).multiply(new BigDecimal("2.2046"));
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(4);
    df.setMinimumFractionDigits(4);
    System.out.println(df.format(value) + " kg\n");
}

public static void main(String[] args) {
    test("0");
    test("1");
    test("3.1");
}

will give you the following output:

0,0000 kg

2,2046 kg

6,8343 kg
Share:
70,429

Related videos on Youtube

Mehrdad Salimi
Author by

Mehrdad Salimi

حیدر بابا دونیا یالان دونیادی

Updated on July 09, 2020

Comments

  • Mehrdad Salimi
    Mehrdad Salimi almost 4 years

    I'm programming a simple java program. I need to get a string from input and divide it into two parts: 1-double 2-string. Then I need to do a simple calculation on the double and send the result to the output with specific precision(4). It works fine, but there is a problem when the input is 0, then it doesn't work properly.

    For example for these input, output will be:

    1 kg
    output:2.2046

    3.1 kg
    output:6.8343

    But when the input is 0, the output should be 0.0000, but it shows 0.0 . What should I do to force it to show 0.0000?

    I read similar post about double precision, they suggest something like BigDecimal class, but I can't use them in this case, my code for doing this is:

    line=input.nextLine();
    array=line.split(" ");
    value=Double.parseDouble(array[0]);
    type=array[1];
    value =value*2.2046;
    String s = String.format("%.4f", value);
    value = Double.parseDouble(s);
    System.out.print(value+" kg\n");
    
  • Aaron
    Aaron over 10 years
    BlackLight's answer looks better.
  • Blacklight
    Blacklight over 10 years
    I already did, replace your last line which prints out your value.
  • Mehrdad Salimi
    Mehrdad Salimi over 10 years
    i used your suggestion, but it doesn't work, as you said BlackLight's answer is what i want, Thanks,
  • Mehrdad Salimi
    Mehrdad Salimi over 10 years
    it worked for me, but please change it to ("#0.0000"), your answer's out put is .0000 , but if we use ("#0.0000") , it show 0.0000, thanks for your help.
  • Aaron
    Aaron over 10 years
    It's impossible for my answer to "not work" :D It's just adding "0"'s to a string. It is a sub-optimal solution though. No point re-inventing wheels!
  • Mehrdad Salimi
    Mehrdad Salimi over 10 years
    i think it doesn't work because of this part value=Double.parseDouble(array[0]);
  • Aaron
    Aaron over 10 years
    @MehrdadSalimi I meant add the padding on the resultant String :) That's what (i presume) decimalformat will be doing internally.
  • Mehrdad Salimi
    Mehrdad Salimi over 10 years
    LOL, Sorry i have english problem, :)
  • Mehrdad Salimi
    Mehrdad Salimi over 10 years
    Thanks for your info, i didn't know how use bigDecimal to set precision.

Related