1

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.

nfg
  • 11
  • 4
  • 2
    If you want a particular format for a number, then pass a string. That's really your only option – Phil Jan 28 '21 at 02:49
  • Unfortunately, there isn't support for bigints in json. So yeah, your only option is to pass them as strings (which is no so bad after all) – Chayim Friedman Jan 28 '21 at 02:52
  • This will be response body to a REST client/consumer. If I pass a string, the field in response body will be Quoted (For a numeric field, it is not quoted). This will thus break the client. – nfg Jan 28 '21 at 03:01
  • If the client needs a number, then it should have no problem with the scientific notation. If it needs a specifically formatted number, then it should be able to accept a string – Phil Jan 28 '21 at 03:06
  • client broke when i pass them 1.2345e-7. Thus this post. But yes fix should be in client, i am just curious if i can fix it my side. Otherwise, Number(1.2345e-7).toFixed(20) will be a string which preserve all digits. I'd just pass that back to him. – nfg Jan 28 '21 at 03:11
  • Can you touch the client code? – Chayim Friedman Jan 28 '21 at 03:12
  • I am exploring if fix can be on my side, if possible. But i can lean on my downstream application developers to get them fix theirs as well. But conceptually, I this shud be fixable my end. I am thinking either my nodejs Express app i intercept response, or use something like jackson-js, see if there's any property option which coerce number formatting (WITHOUT converting to a string type). – nfg Jan 28 '21 at 03:15
  • For a more condensed version, try using unicode to determine the parts of the string. This would be more ideal for extremely large numbers. – merlin Jan 28 '21 at 03:22
  • You can use your own version of `JSON.stringify()` (or a library one) – Chayim Friedman Jan 28 '21 at 03:27
  • stringify() returns a string, i'd have problem with quotation again. I also thought i was going to override toString(), not much use. As when i return from my REST method, i am returning a JSON javascript object, not string. Thus i am thinking with an Express app there may be some where i can intercept response and do some manipulation there. – nfg Jan 28 '21 at 03:52
  • I'd suggest you to override `Number.prototype.toJSON`, but it does not work either. Nor specifying a replacer. I think the only solution is to rewrite `JSON.stringify()`. – Chayim Friedman Jan 28 '21 at 03:53

0 Answers0