www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can I stop D from dropping decimals in strings

reply Tim <tim.oliver tutanota.com> writes:
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?
Feb 02
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
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?
Feb 02
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Feb 02
parent Andre Pany <andre s-e-a-p.de> writes:
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
Feb 02