-1
double value1 = 1000
double value2 = 2563.59
System.out.println("value1 ="+String.format("%.2f",value1));
System.out.println("value2 ="String.format("%.2f",value2));

Output 
value1 = 1000.00
value2 = 2563.60

This returns 1000.00 and 2563.60 but the type of output changes to string.

I want 1000.00 and 2563.60 as the output without changing the datatype.

Can anyone help me?

The dtatype must be double itself

Fida
  • 145
  • 1
  • 4
  • 14

5 Answers5

0

Don't use String.format(..) if you don't want it to change to a string.

Please refer to Decimal Format

// 2 places of decimal
DecimalFormat formatter = new DecimalFormat( "#.00" );

System.out.println(formatter.format(1.23));    // 1.23
System.out.println(formatter.format(1));       // 1.00
System.out.println(formatter.format(1.234));   // 1.23
Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
0

Note that double types store values, not strings. There are gonna be some cases where rounding it to 2 decimal places changes nothing. For example, the value 1. If you do a String.format on 1, it will become 1.00. However, 1 and 1.00 are the same value, so turning 1 to 2 decimal places makes it 1 again. double thinks that 1 and 1.00 are the same value (I mean, they really are, it's just formatted differently).

Also note that double values are inaccurate. It might appear as though it is 1.23 but in reality it might be something like 1.2300000000000001.

To correct a double to 2 d.p., all you have to do is to convert the formatted string back to a double:

String str = String.format("%.2f",value1));
double result = Double.parseDouble(str);
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

why you are using string.format in first place?

DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(value1));

you need to import java.text.DecimalFormat; for this

Omniverse10
  • 134
  • 2
  • 15
0

Using BigDecimal.setScale() and using rounding mode ceiling

    double value1 = 1000;
    double value2 = 2563.59;

    System.out.println("Before");
    System.out.println("value1 = " + value1);
    System.out.println("value2 = " + value2);

    value1 = BigDecimal.valueOf(value1).setScale(1, RoundingMode.CEILING).doubleValue();
    value2 = BigDecimal.valueOf(value2).setScale(1, RoundingMode.CEILING).doubleValue();

    System.out.println("After");
    System.out.println("value1 = " + value1);
    System.out.println("value2 = " + value2);
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
0

In anything that uses limited-precision, binary floating-point numbers -- which is the default for Java and most programming languages -- the concept "number of decimal places" only ever applies to display. The internal binary representation has no concept of "decimal places" because the data is not decimal.

Consequently, the number of decimal places applies only to how data is displayed, and it is irrelevant what data types may be used to do the display.

When questions arise about rounding decimal numbers, it's often because the programmer has not fully understood how float-point math works.

Kevin Boone
  • 4,092
  • 1
  • 11
  • 15