Decimal places

This is a discussion on Decimal places within the Windows Programming forums, part of the Platform Specific Boards category; Hello I use sprintf_s() to convert numbers to strings however I have a problem with decimal places. What do I ...

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

    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. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    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
    Quote Originally Posted by matsp View Post
    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. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Quote Originally Posted by iMalc View Post
    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. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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!
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Salem View Post
    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.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    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).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Salem View Post
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. %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
  2. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 10:03 AM
  3. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 11:56 AM
  4. how to set decimal places Borland C++ 3.0 compiler???
    By neo_phyte in forum C++ Programming
    Replies: 10
    Last Post: 08-31-2006, 05:08 PM
  5. 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