1
  • I was working with integration, where i need to send my internal data to Core system. I have came across a strange issue while converting large number(Double) into JSON.
  • Sales-force automatically converting number into exponent. Below is sample json i have to trying to resolve,In real scenario my JSON is holding very large content. Does any know how to resolve this issue?

Sample Json format trying to generate:

{ "Amount":45454544.00 }

Example 1:

Double num1=45454544.00;
String str= '{"Amount":' + num1 + '}';
system.debug(str);

Example 2:

Map<String,Double> num2=new Map<String,Double>{'Amount'=>45454544.00};
system.debug(JSON.Serialize(num2));

Output for the above code:

{"Amount":4.5454544E7}

Core system is throwing an error, because it unable to resolve exponent form.

Question Updated:

Using decimal will still has an issue while converting larger number.

Double num1=45454544545546556.23;
Decimal num3= (Decimal)num1;
String str= '{"Amount":' + String.valueOf(num3) + '}';
system.debug(str);

output: {"Amount":4.545454454554656E+16}

1 Answer 1

3

You should use Decimal data type:

Map<String, Decimal> num2 = new Map<String, Decimal>{ 'Amount' => 45454544.00 };
system.debug(JSON.Serialize(num2));

Outputs:

{"Amount":45454544.00}

Read this question Decimal or double for a more in depth analysis.

4
  • I have updated question.. Still issue with larger number...
    – NITHESH K
    Nov 27, 2020 at 13:34
  • You need to store the original number in a variable of Decimal Data type, Double is unable to handle large numbers, so it stores in exponential value. eg: Decimal num1=45454544545546556.23;
    – Raul
    Nov 27, 2020 at 13:38
  • but when i convert from double to decimal it lost decimal point also for large number...
    – NITHESH K
    Nov 27, 2020 at 13:39
  • Yes it would get lost, hence you should use Decimal type
    – Raul
    Nov 27, 2020 at 13:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .