function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sdusdu 

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'

 

 

sdusdu

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

Naidu PothiniNaidu Pothini
Decimal d = 12345.67;

String s = String.valueOf(d);

Decimal k = Decimal.valueOf(s);

 Try this.

sdusdu

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 PothiniNaidu Pothini
Decimal d = 12345.67;
String s = d.format();
 
Decimal k = Decimal.valueOf(s.replace(',',''));

 try this.

sdusdu

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.

dphilldphill
Decimal d = 12345.67;
String s = d.format();
 
Decimal k = Decimal.valueOf(s.replace(',','.').replaceAll(' ', ''));

 

sdusdu

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

dphilldphill
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 PothiniNaidu 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_KimbleStuart_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.