Search code examples
javaandroidnumber-formatting

How can I do format a number to with dots


I have a 5 digit number and I want to put a dot/comma in the last step of this number.

1234 to 123,4
2564 to 256.4

I tried this but it wasn't

int val=1234;
NumberFormat number = NumberFormat.getInstance();
number.setMaximumFractionDigits(3);
String output = number.format(val);

Can you help me, please? Thanks in advance.


Solution

  • If you're dealing with integers the easiest way achieve this is to just divide it by 10 and cast it to a double.

    int a = 1234;
    double b = (double) a/10;
    

    This will turn 1234 into 123.4.

    EDIT: This answer is based on your exact question. Putting a comma before the last digit.