1

I'm using an api from a third party and one of the data fields I need to send in the body is a float with 8 decimals places, it cant be a string, but when I send the body the number is sent as a exponential value like 8.9e-7, is there a way to send it as a float?

JSON.stringify({float:0.00000078})

"{"float":7.8e-7}"

with toFixed(8) returns a string.

API example

fetch("api-url", {
  method: "POST",
  headers: {
    "Content-type": "application/json"
  },
  body: JSON.stringify({ float: 0.00000078 })
})
Phil
  • 157,677
  • 23
  • 242
  • 245
ribas
  • 97
  • 1
  • 8
  • 1
    It is a float. But you could format it – GetSet Jan 21 '21 at 23:50
  • How are you creating and sending this number? `JSON.stringify({float:0.12345678})` produces `{"float":0.12345678}` for me – Phil Jan 21 '21 at 23:50
  • 1
    @Phil it does not work for JSON.stringify({float:0.00000078}) – ribas Jan 21 '21 at 23:57
  • 2
    @Phil `.toFixed()` returns a string. – Marco Jan 21 '21 at 23:59
  • 2
    I don't think `JSON.stringify` supports disabling scientific notation for floats. If the API wants `8` decimal places, then it really should accept a string. – Marco Jan 22 '21 at 00:03
  • perhaps show HOW you are "sending" it - or maybe build a URL string with it in there (if that is the "api" since you did not indicate much there – Mark Schultheiss Jan 22 '21 at 00:04
  • 1
    @MarkSchultheiss open the console and try to JSON.stringify({"float":0.00000078}), the result is a exponential number, I can't use toFixed(8) because it returns a string, the api or the format of the data is not the problem – ribas Jan 22 '21 at 00:09
  • @ribas perhaps you understood my comment - build the url as a string and submit it (if it is a web api of some sort) – Mark Schultheiss Jan 22 '21 at 00:16
  • @Phil unfortunately yes, I need to stringify the object before sending it, it does not accept a string or the exponential number – ribas Jan 22 '21 at 00:18
  • Maybe this will help https://stackoverflow.com/questions/1685680/how-to-avoid-scientific-notation-for-large-numbers-in-javascript – Cristik Jan 22 '21 at 00:21
  • @Cristik thank you but it returns a string – ribas Jan 22 '21 at 00:35
  • 3
    @ribas yes, but as others have said, you might not have a choice other than a string. But on the other hand why do you want to avoid this format? I assume the JSON decoding on the other side will decode the same value even if sent in the scientific form. – Cristik Jan 22 '21 at 07:18
  • 1
    Just do the JSON yourself, JSON is just a STRING with a specific form and format `body: "{"float":0.00000078}"` – Mark Schultheiss Jan 22 '21 at 14:18

1 Answers1

0

The exponent notation is part of the JSON spec and, in this case, it's only used for shortening the output. There is no reason to worry about it, unless that "API from a third party" doesn't accept JSON.

If the number didn't have zeros up to the decimal point, that syntax wouldn't be used. For example:

JSON.string(0.00000078); // 7.8e-7
JSON.string(0.10000078); // 0.10000078
D. Pardal
  • 6,173
  • 1
  • 17
  • 37