-1

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.

Arvind Kumar Avinash
  • 62,771
  • 5
  • 54
  • 92
theoyuncu8
  • 63
  • 7

2 Answers2

3

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.

duplxey
  • 131
  • 7
1

You can add any character before last digit using this code

String str = String.valueOf(1234);
Integer position = str.length()-1;
String newVal = str.substring(0, position) + "." + str.substring(position);
System.out.println(newVal); //123.4
Dilan
  • 2,333
  • 7
  • 21
  • 30