-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
  • 71,965
  • 6
  • 74
  • 110
theoyuncu8
  • 73
  • 8

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
  • 260
  • 3
  • 11
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,610
  • 7
  • 23
  • 33