1

I have a currency field in object. I am assigning value to it in apex

For US users: the currency format is 241,500.00 so when i apply decimalof on this i am getting 241500000.00.

But for Eur users: the currency format is 241.500.00000, so when i apply decimalof on this i am getting Invalid decimal: 241.500.00000

String s = 241.500.00000 or 241,500.00 ; 
mar.flowvalue__c = decimal.valueOf(s);

Could you please help me to resolve this issue

1

Unfortunately while the Decimal class has a format method:

format()
Returns the String value of this Decimal using the locale of the context user.

there is no equivalent parse method that respects the user's locale.

The approach that side-steps the problem is to use things like <apex:inputField/> that does respect the locale in the string conversion and stores a Decimal value after the parsing rather than a String. Otherwise you will have to write your own locale aware parse method.

1

Decimal.valueOf isn't locale-aware. Consider binding to a Decimal directly on the page, as this automatically handles conversion to/from the user's locale for you:

<apex:input type="number" value="{!mar.flowvalue__c}" />

Or

<apex:inputText value="{!mar.flowvalue__c}" />

Or

<apex:inputField value="{!mar.flowvalue__c}" />

Any of the above methods will be locale aware and do the appropriate conversion for you.

If you want to assign a known value in your code, use a literal decimal value:

mar.flowvalue__c = 241500.00;

Even if you don't want to bind directly to the field, you can still use a Decimal value:

public Decimal flowValue { get; set; }

<apex:input type="number" value="{!flowValue}" />

Or

<apex:inputText value="{!flowValue}" />

Basically, the point is, bind to a Decimal value, and you'll automatically get the benefits of locale-aware Decimal parsing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.