Float is getting converted to either int or string. But we need to pass it as float.

Hi @dchiesa1 ,

 

We're working on a response payload transformation. We're using javascript to do it as the response payload consists of lots of arrays. Here there's a field where we receive it as float and we need to pass a float to source systems.

 

But JS is somehow converting the float to int when decimal part contains only zeros. For example, 

if responsePayload.x = 5.009, our js is passing it as it is.

But if responsePayload.x = 5.00, our js is converting it into just 5(which is not acceptable). Is there any way to stop our float getting converted into into int when decimal part contains only zeros? Could someone please help?

 

Attaching a sample code for reference:

var payload = JSON.parse('{"name":"John", "age":30.000, "city":"New York"}');
var floaT = 0.00;

var page = payload.age.toFixed(5);
console.log(page)


let finalResponse={
name: payload.name,
age: payload.age,
hi: floaT
}
frs = JSON.stringify(finalResponse)
console.log(frs)

Response:

30.00100
{"name":"John","age":30,"hi":0}

 

P.S. We dont want to convert tis into string, for example "30.000". We just need to pass as float along the trailing zeros.

 

Thanks in advance!

Manoj T.

2 1 74
1 REPLY 1

In JSON, as in JavaScript, and UNLIKE Java or C# etc, there are not distinct numeric types for representing floats and ints (cite). There is only Number.

5.00 is the same as 5. 

I think a system that is insisting on decimal points within JSON, is wrong.  It's unnecessarily rigid, or it's applying JSON incorrectly. 

If you really need Apigee to format numbers in a JSON payload with a minimum number of decimal digits, then you will probably need to resort to custom regex to manipulate the output of JSON.stringify(),  to make that happen. 

Or you could try with JSON.parse and a reviver function. The following is VERY HACKY, but works to emit numbers with decimal digits, when run in Rhino for simple (flat) objects. If you have something more complex , then I don't have any good suggestions for you. 


print("\n==================================");
var c = {
  keyid: "cc544724",
  meas: 17.1,
  expires: true,
  quality: 42,
  notbefore: "10s"
};

var json = JSON.stringify(c, null, 2);
print(json); // no decimal digits

var DESIRED_DECIMAL_PLACES = 3;
var modifiedString = "";

function reviver(key, value) {
  if (key) {
    if (modifiedString == "") {
      modifiedString = "{\n";
    } else {
      modifiedString += ",\n";
    }
    modifiedString += '  "' + key + '": ';
    var t = typeof value;
    if (t == "number") {
      // force decimal digits
      var output = value.toFixed(DESIRED_DECIMAL_PLACES);
      modifiedString += output;
    } else if (t == "boolean") {
      modifiedString += value;
    } else {
      modifiedString += '"' + value + '"';
    }
  }
  return value;
}
const unusedObject = JSON.parse(json, reviver);
print(modifiedString + "\n}"); // includes decimal digits

The output is: 

{
  "keyid": "cc544724",
  "meas": 17.100,
  "expires": true,
  "quality": 42.000,
  "notbefore": "10s"
}