1

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.

Pavan Andhukuri
  • 1,547
  • 3
  • 23
  • 49
  • Does this answer your question? [Keep trailing or leading zeroes on number](https://stackoverflow.com/questions/70616370/keep-trailing-or-leading-zeroes-on-number) – Mostafa Fakhraei Jan 16 '23 at 12:16

2 Answers2

0

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.

-1

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.