digitalmars.D.learn - How can I stop D from dropping decimals in strings
- Tim (5/5) Feb 02 2021 Hi all,
- Adam D. Ruppe (5/9) Feb 02 2021 This depends on the library you are using for serialization, but
- Steven Schveighoffer (9/14) Feb 02 2021 It totally depends on the library. As Adam says, 0.0 and 0 are generally...
- Andre Pany (8/23) Feb 02 2021 I had a similiar issue. For me the issue was, std json "floating"
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 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?
Feb 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
Feb 02 2021
On Tuesday, 2 February 2021 at 23:10:50 UTC, Steven Schveighoffer wrote:On 2/2/21 5:27 PM, Tim wrote: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 AndreHi 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 2021