2

I have to send JSON data from a NodeJS app to a Scala server. The Scala server expects a value to be a double but I am having an issue when the value I have to send is sharp and thus have .00 as decimals. When sent, it becomes an integer which obviously generates an error on the server side, which expects a double.

The question is, is there a way to force a float in NodeJS to stay a float with .00 (or .0) as dicimals without becoming a string (which happens with .toFixed(2) for instance) ?

Basically, I want this to do what I want:

var division = (2.0/1.0); // is a sharp float thus becomes an integer (javascript "number" with no decimals) => 2 
var stringNumber = parseFloat(division).toFixed(2); // is unfortunately a string => "2.00"
var floatNumber = magic(stringNumber); // would be a float => 2.00
var test = (typeof(floatNumber) === 'number'); // would be true!

Thank you for your help

mentinet
  • 744
  • 3
  • 9
  • 23
  • what about adding a .00000001? – lonewarrior556 May 18 '16 at 20:54
  • That is tricky. If you're in control of the server, you'd better deal with the problem on that side instead. Not supporting implicit integer to double conversion is a shame. – E_net4 May 18 '16 at 20:57
  • Its probably a bigger deal that scales generates errors on server side for receiving the right 'number'... – lonewarrior556 May 18 '16 at 21:09
  • I can't change the server. I must try with the addded decimals like this because I don't think the server would like that. If you tell me that is not possible in JS to have a float (type of number) with `.00` decimals, than I'll find a workaround. Thanks for your help. – mentinet May 18 '16 at 21:41

0 Answers0