Converting double to string with no

#1

I’m a little baffled by the functions used to convert double values to strings. If I specify a number of decimal places > 0, I get a full number, e.g. String(value, 2)

2260884909.49

but if I specify String(value,0) I get

2.26088e+09

thereby losing many of the digits.

Looking though the code I can see technically what is happening (see the function doubleToString in juce_String.h) but I don’t understand the motivation for this design decision. The underlying function doubleToString is not public and seems only accessible via the String functions so there’s no way to tell it how many digits you want in the result.

Curiously…

0 Likes

#2

I’ll just mention you can use std::to_chars in case you have C++17 available.

0 Likes

#3

You misunderstand. Once I realized what was going on, I was able to trivially deal with it. But I’m wanting to understand why it was implemented this way. My experience with Jule’s programming style is that nothing gets done without a good reason.

0 Likes

#4

Well 0 is the default value, in which case JUCE just falls back to using the C++ standard library default format. If you’ve not specified the number of decimal places then the assumption is that you don’t mind how the number is formatted.

0 Likes

#5

One would like to be able to see the entire number even if there are no decimals in it. There’s a big difference between not having any decimal places and having half of the digits of the number missing!

0 Likes

#6

This is something JUCE could do better. It would be nice to have a whole suite of double <-> String conversion functions. However, changing the behaviour of this particular case would cause too much trouble for existing applications.

0 Likes

#7

Again, I was just trying to understand the use case where setting the number of decimal places to 0 causes the precision of the underlying number to be shortened. This just seems like a bug.

0 Likes

#8

And I’m eager to know why asking for 0 decimal simply doesn’t give you… zero decimal!

It’s quite counter-intuitive.

I’d be OK using String(myDouble) for the default C++ standard library default format, but String(myDouble, 0) explicitly says I want NO decimal.

0 Likes