Here's the problem, as soon as a numeric field >6 decimal places, when print we get the scientific notation. For example, 1.2345e-7 (But 1.2345e-6 no such problem). However I don't want to do rounding parseFloat(new Number(1.2345e-7 ).toFixed(7)) before passing on my values to my consumer. Note,
A. Number/BigNumber.toFixed returned a string, not a float As with all these toFixes variants: How to avoid scientific notation for large numbers in JavaScript?
B. ParseFloat convert it back to a float but again you'd have scientific notation
Simple example.
var p2c = require('class-transformer');
class Fund {}
let fundJson = {
"fund_code": "ABCDE",
"ccy": "USD",
"amount_fund_ccy": 0.00000012345,
"amount_trx_ccy": 1.2345e-7
};
let fund = p2c.plainToClass(Fund, fundJson);
let fundJson2 = JSON.stringify(fund);
If you print it,
fundJson
{fund_code: 'ABCDE', ccy: 'USD', amount_fund_ccy: 1.2345e-7, amount_trx_ccy: 1.2345e-7}
fund
Fund {fund_code: 'ABCDE', ccy: 'USD', amount_fund_ccy: 1.2345e-7, amount_trx_ccy: 1.2345e-7}
fundJson2
'{"fund_code":"MLHE1U","ccy":"USD","amount_fund_ccy":1.2345e-7,"amount_trx_ccy":1.2345e-7}'
The above is an example of a response in my REST API. I want to avoid scientific notation but not sure how to go about to do that. I can Number(1.2345e-7).toFixed(20) (to 20 decimals and preserve all digits) and pass back to my REST client as string, but the amount field will be quoted, this will break my REST consumers downstreams.