Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
sdu

# parse decimal bug?

Why following code does not work in Sweden locale?

Decimal d = 12345.67;
String s = d.format();
Decimal dd = Decimal.valueof(s); // throws 'System.TypeException: Invalid decimal: 12 345,67'

sdu

Basically I need an unformat method that works as reverse of format and works for any locale.

Naidu Pothini
```Decimal d = 12345.67;

String s = String.valueOf(d);

Decimal k = Decimal.valueOf(s);```

Try this.

sdu

I know this would work. But what I need is to display numbers formatted according to user's locale (thus use format()), and be able to parse numbers entered by user in their locale.

Naidu Pothini
```Decimal d = 12345.67;
String s = d.format();

Decimal k = Decimal.valueOf(s.replace(',',''));```

try this.

sdu

Thanks, but it does not work, first it still chokes on space (the thousand separator), second I cannot remove comma, because it is the decimal point in Sweden locale.

dphill
```Decimal d = 12345.67;
String s = d.format();

Decimal k = Decimal.valueOf(s.replace(',','.').replaceAll(' ', ''));```

sdu

This would work for Sweden locale, but what I need is code that works for any locale.

dphill
```Decimal d = 12345.67;
String s = d.format();

Decimal k;
if (UserInfo.getLocale() == SWEDISH_LOCALE)
k = Decimal.valueOf(s.replace(',','.').replaceAll(' ', ''));
else
k = Decimal.valueOf(s);```

Naidu Pothini
```Decimal d = 12345.76;

String s = d.format();

Integer i = s.length();

String stx = s.subString(0, i-3).replace(' ','').replace(',','').replace('\'','').replace('.','') + '.' +s.subString(i-2,i);

Decimal d1 = Decimal.valueOf(stx);```

I guess this would work for all locales.

Stuart_Kimble

Hi - did you ever get a sensible answer to this problem? i.e. one that didn't rely on string manipulation to replace commas, spaces and dots.