How can I stop D from dropping decimals in strings

Andre Pany andre at s-e-a-p.de
Wed Feb 3 05:29:31 UTC 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


More information about the Digitalmars-d-learn mailing list