# Decimal places

• 09-28-2007
Gordon
Decimal places
Continuation of http://cboard.cprogramming.com/windows-programming/93851-double-type.html

This is the code I was advised and which works well.
Code:

```double iTotal; char Total[64]; TCHAR chTotal[64]; sprintf(Total, "%f", iTotal); wsprintf(chTotal, "%s", Total); SetDlgItemText(hwndDlg, nControl, chTotal);```
It return the value with 6 decimal places even if all of those decimal places are 0. So the result of 36 looks like 36.000000
Also if I try to type more then 6 decimal places it shows no more then 6.

What shall I do now with it? Shall I try some other conversion of types or a method which return only certain number of decimals?
• 09-28-2007
matsp
There is a default for %f on how many decimal places it displays, you can control it by specifiying something like "%6.2" - this will give you two decimal places, and the number will be filled with spaces on the right side to make it 6 positions in total.

If you ONLY want decimal places, "%.2f" will give you two decimal places.

I hope this helps.

--
Mats
• 09-28-2007
Gordon
Yes it work all right thx...

But now I realize there is a problem that the number of decimal places is not constant. I am wondering how to use a variable carring the number of decimals in this case.
• 09-28-2007
matsp
Assuming your sprintf is "compatible", you could do:

Code:

```int positions, decimals; ... positions = 6; decimals=2; .... sprintf(str, "%*.*f", positions, decimals, value); ....```
--
Mats
• 09-28-2007
Gordon
Cool now it seems to work without a problem