1. ## Decimal places

Hello

I use sprintf_s() to convert numbers to strings however I have a problem with decimal places. What do I do if I dont know in advance how many decimal places will a number have and I want to show decimal places only if there are any.

2045 / 756 I have no idea how many decimal places will the answer have. What do I do to find out?

If I use only

Code:
```ValueX = 2045 / 756;
sprintf_s(lpText, "x = %.*f", 2, ValueX);```
That will end up with two decimal places even if they are both zeroes... In this case it will work but what if I had 9 / 3...

Thank you

2. Unfortuantely, that's a tricky one. If you know that you'll never have more than 2 decimals in your output, something like this would work:
Code:
```ValueX = 2045 / 756;
if (fabs(ValueX - (int)ValueX) < 0.001)
... No decimals.  ;
else
... Have decimals ... ;```
--
Mats

3. Originally Posted by matsp
Unfortuantely, that's a tricky one. If you know that you'll never have more than 2 decimals in your output, something like this would work:
Code:
```ValueX = 2045 / 756;
if (fabs(ValueX - (int)ValueX) < 0.001)
... No decimals.  ;
else
... Have decimals ... ;```
--
Mats
Thx it covers its purpose

4. You could scan backwards over your formatted string afterwards and turn zeros back into nulls, and then remove the dot if it then ends in a dot.

5. Originally Posted by iMalc
You could scan backwards over your formatted string afterwards and turn zeros back into nulls, and then remove the dot if it then ends in a dot.
Thank you that is a very good idea, I will certainly try to use that

6. Isn't there a standard function to check out how many decimal places a float has? If there isn't I'm gonna make one!

7. Constants (in float.h) like DBL_DIG tell you how many significant digits a floating point type has.

You might think you have
float pi = 3.1415926535897932384626433832795;
But since FLT_DIG on most systems is 6, it's only the red part which has any real significance. The rest (however accurate it may appear) is just noise.

8. Originally Posted by Salem
Constants (in float.h) like DBL_DIG tell you how many significant digits a floating point type has.

You might think you have
float pi = 3.1415926535897932384626433832795;
But since FLT_DIG on most systems is 6, it's only the red part which has any real significance. The rest (however accurate it may appear) is just noise.
It only specifies how many digits are guaranteed to be exact, the rest might be accurate too, depending on the calculation.

9. Only if you're fortunate enough to use a compiler which might keep the intermediate result in a double or a long double for the duration.

But as soon as it's stored in a float, any extra accuracy is gone for good.

But that kind of assumption (and resultant implementation defined behaviour) will lead you on a merry dance (see sig).

10. Originally Posted by Salem
Constants (in float.h) like DBL_DIG tell you how many significant digits a floating point type has.

You might think you have
float pi = 3.1415926535897932384626433832795;
But since FLT_DIG on most systems is 6, it's only the red part which has any real significance. The rest (however accurate it may appear) is just noise.
On x87 systems, some constants, like pi, are encoded in the FPU itself to 82 bits, adn rounded at execution time according to the currently active rounding model to 80 bits. All FPU operations are 80 bit internally, then converted to single double or extended precision when it is transferred to memory.