Decimal places

This is a discussion on Decimal places within the Windows Programming forums, part of the Platform Specific Boards category; Continuation of http://cboard.cprogramming.com/showthread.php?t=93851 This is the code I was advised and which works well. Code: double iTotal; char Total[64]; TCHAR ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    Decimal places

    Continuation of Double type

    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?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Assuming your sprintf is "compatible", you could do:

    Code:
    int positions, decimals;
    ...
    positions = 6;
    decimals=2;
    ....
    sprintf(str, "%*.*f", positions, decimals, value);
    ....
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Cool now it seems to work without a problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 06-08-2008, 09:04 AM
  2. %g doesn't print as many decimal places as %f
    By octoc in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 PM
  3. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 11:56 AM
  4. Replies: 10
    Last Post: 06-12-2002, 03:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21