Using express, is it possible to return 0.0
(as a number) in response JSON?
What we are observing is, if the value is 0.0
, then Javascript is keeping only 0
and response JSON just has 0
.
We have a consuming system which is implying the data type of the json property based on data and it is wrongly implying the field to be Long instead of Decimal.
CodePudding user response:
you can use .toFixed()
to set the number of decimals. So for example
var number = 0.000;
console.log(number); // <--- Prints only 0
var preciseNumber = parseFloat(0).toFixed(4); // 4 inside the .toFixed(4) is number of places after decimal point
console.log(preciseNumber); // <--- Prints '0.0000'
Do note that the preciseNumber
is a string now.
CodePudding user response:
you can use .toFixed()
to set the number of decimals. So for example
var number = 0.000;
console.log(number); // <--- Prints only 0
var preciseNumber = parseFloat(0).toFixed(4); // 4 inside the .toFixed(4) is number of places after decimal point
console.log(preciseNumber); // <--- Prints '0.0000'
Do note that the preciseNumber
is a string now.