0

I have a String which is taken from a JSON file. I need to convert it to double or int and then round it to 2 decimal places and if its an int, round to 1 digit

for example: if it is 76424443 it should be rounded to 76.4 or if it is 7.122345936298 should be rounded to 7.12

This is what I have done so far.

int value = Integer.parseInt(value1);
value1 = String.valueOf(Math.round(value));

I also do not know if the value is double or int, should be something that works with both.

Alex
  • 255
  • 2
  • 5
  • 10
  • 2
    Did you mean `76.424443`? Double will handle "int" values. – keyser Nov 28 '13 at 22:06
  • Don't understand how 76424443 should be converted to 76.4. May you explain a little more about what you want ? –  Nov 28 '13 at 22:08
  • @ᴋᴇʏsᴇʀ as it is 76 millions, cant I show it 76.4 m ?! – Alex Nov 28 '13 at 22:16
  • @Gaëtan because it is a big unnecessary number, they normally show it like 76.4 m that means 76 millions and something... . That was what I wanted but if it is not possible, then I only need something to convert string to double and only show 2 floating points and if it has not floating points then only shows first 2 digits. like instead of 76424443 shows 76. – Alex Nov 28 '13 at 22:20
  • 1
    @DannyJj Those are two separate rounding problems. You should edit your question to say 76.4M to avoid more confusion. – keyser Nov 28 '13 at 22:25

2 Answers2

4

Try this code :

Double value = Double.parseDouble(value1);
String value1 = "0";
if(value != null){
    if(value == (double) Math.round(value)){
        if(value/1000000000 > 1.0){
            value1 = String.format("%.1f G", value/1000000000);
        }
        else if(value/1000000 > 1.0){
            value1 = String.format("%.1f M", value/1000000);
        }
        else if(value/1000 > 1.0){
            value1 = String.format("%.1f K", value/1000);
        }
        else{
            value1 = String.format("%.1f", value);
        }
    }
    else{
        value1 = String.format("%.2f", value);
    }
}
  • Is it possible to add % sign if it has floating point? I tried this but did not work. `value1 = String.format("%.2f %", value);` – Alex Nov 28 '13 at 22:59
  • In String.format, % is a special character. You should escape it and do `value1 = String.format("%.2f %%", value);` –  Nov 28 '13 at 23:00
0

Consider using a BigDecimal. It will let you round your values to the precision you like. Have a look at this answer: Set specific precision of a BigDecimal

If you cast a decimal value to integer then you will lose the decimal part. If that is the case then why bother rounding at all ?

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
  • Thanks for you answer, it seems does the job, but how should I change it to work in my case. I mean what does 12 mean that the user uses in that question? – Alex Nov 28 '13 at 22:25
  • Have a look at the documentation. It will provide detailed info. Plus, search for tutorials. – An SO User Nov 28 '13 at 23:13