Thread overview
How can I stop D from dropping decimals in strings
Feb 02, 2021
Tim
Feb 02, 2021
Adam D. Ruppe
Feb 03, 2021
Andre Pany
February 02, 2021
Hi all,

I have to serialize an array like [0.0, 0.0, 0.0] to a Json object. During this process, the serializer creates a string of the array, but it creates "[0, 0, 0]", dropping the decimal. How can I stop this?
February 02, 2021
On Tuesday, 2 February 2021 at 22:27:53 UTC, Tim wrote:
> I have to serialize an array like [0.0, 0.0, 0.0] to a Json object. During this process, the serializer creates a string of the array, but it creates "[0, 0, 0]", dropping the decimal. How can I stop this?

This depends on the library you are using for serialization, but 0 and 0.0 are the same thing in json anyway so it shouldn't matter for data interchange.

Which lib you using?
February 02, 2021
On 2/2/21 5:27 PM, Tim wrote:
> Hi all,
> 
> I have to serialize an array like [0.0, 0.0, 0.0] to a Json object. During this process, the serializer creates a string of the array, but it creates "[0, 0, 0]", dropping the decimal. How can I stop this?

It totally depends on the library. As Adam says, 0.0 and 0 are generally interchangeable, but reality says they are sometimes not (I have had my share of issues with vibe.d Json treating 1.0 as Json.Type.float_ and 1 as Json.Type.int_, where it doesn't allow you to get the int version as a float).

It would help to have a better understanding of why you need the .0 to appear.

-Steve
February 03, 2021
On Tuesday, 2 February 2021 at 23:10:50 UTC, Steven Schveighoffer wrote:
> On 2/2/21 5:27 PM, Tim wrote:
>> Hi all,
>> 
>> I have to serialize an array like [0.0, 0.0, 0.0] to a Json object. During this process, the serializer creates a string of the array, but it creates "[0, 0, 0]", dropping the decimal. How can I stop this?
>
> It totally depends on the library. As Adam says, 0.0 and 0 are generally interchangeable, but reality says they are sometimes not (I have had my share of issues with vibe.d Json treating 1.0 as Json.Type.float_ and 1 as Json.Type.int_, where it doesn't allow you to get the int version as a float).
>
> It would help to have a better understanding of why you need the .0 to appear.
>
> -Steve

I had a similiar issue. For me the issue was, std json "floating" was raising an exception while reading e.g  number 1 instead of 1.0.
Ths solution was to use "get!double" instead.

Kind regards
Andre