20

Possible Duplicate:
Round a double to 2 significant figures after decimal point

how to convert double to 2 number after the dot ?

for example:

double x=123.45678;

i need that x=123.45 (in java for android)

thanks in advance

Community
  • 1
  • 1
goldsoft
  • 659
  • 2
  • 8
  • 11
  • Do you want to convert `x` from `double` to different type (such as `String` with only two digits after the dots printed in it)? Or do you want to keep the double but truncate that part? – sinelaw Sep 14 '11 at 11:47
  • follow this link http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – Yashwanth Kumar Sep 14 '11 at 11:47

6 Answers6

40
x = Math.floor(x * 100) / 100;
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 10
    i used this method but if the number has example like 6.50 it shows 6.5. how i can add the zero? – George Panayi Mar 08 '13 at 01:50
  • @GeorgePanayi, you mean string formatting, see https://stackoverflow.com/questions/10959424/show-only-two-digit-after-decimal. First check whether `Math.abs(x) % 1.0 < 0.005;`. If it is whole, format `x` as whole number, and if fractional, then use `new DecimalFormat("###########0.00").format(x);`. – CoolMind Nov 29 '19 at 13:35
19

try this code

double x=123.45678;
DecimalFormat df = new DecimalFormat("#.##");
String dx=df.format(x);
x=Double.valueOf(dx);
Balaji.K
  • 8,745
  • 5
  • 30
  • 39
  • Exception in thread "main" java.lang.NumberFormatException: For input string: "123,46" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at java.lang.Double.valueOf(Double.java:502) – iXô May 18 '16 at 08:25
  • I uses it but it shows value in this form "7016,01" instead of "7016.01". It show a qoma instead of dot. – sajid45 Mar 17 '17 at 09:56
  • replace , by . in dx: x=Double.valueOf(dx.replace(',', '.')); – Riadh Sep 13 '17 at 14:30
  • @sajid45, how can it be? Does it depend on regional settings? – CoolMind Nov 27 '19 at 11:42
14

use this,

  NumberFormat formatter = new DecimalFormat("#0.00");
 x=formatter.format(x);
ilango j
  • 5,967
  • 2
  • 28
  • 25
9

Use NumberFormat class for correct formatting of numbers.

Specifically in your case you can do the following. Although this is not ver L10N friendly.

mNumberFormat = NumberFormat.getInstance();
mNumberFormat.setMinimumFractionDigits(2);
mNumberFormat.setMaximumFractionDigits(2);
EvilDuck
  • 4,386
  • 23
  • 32
8

The best way to format a double is with the NumberFormat class: http://developer.android.com/reference/java/text/NumberFormat.html

You'll be doing something like this:

NumberFormat nf = NumberFormat.getInstance(); // get instance
nf.setMaximumFractionDigits(2); // set decimal places
String s = nf.format(x);

You can also cast it back to a double.

SBerg413
  • 14,515
  • 6
  • 62
  • 88
2

If you want to get two digits correctly rounded, I would go with BigDecimals:

        BigDecimal bd = new BigDecimal(x);
        System.out.println(bd.setScale(2,BigDecimal.ROUND_HALF_EVEN).toPlainString());
Kris
  • 5,714
  • 2
  • 27
  • 47