Thread: How to Convert value from double to long type in C ?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Question How to Convert value from double to long type in C ?

    Hello Everyone,

    I searched on internet but could not find the answer to meet my requirement, I am facing the Conversion problem from double to long without truncation.

    In my VC++ application there are some complex tax calculation logic's, and we have to do calculation for all double values and assign those to long type without any truncation.

    So, please help me, if having some way for value conversion from double to long in C language ?


    Thanking You.
    Prat.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    How many cents are in a dollar?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You've already lost if you're doing financial calculations in floating point.

    Also, since a long is typically 32-bits, and the mantissa of a double is 52 bits, you're always going to lost a lot of bits.

    You could use a long long if you wanted, which should be good enough for all the sane currencies in the world.
    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.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by whiteflags View Post
    How many cents are in a dollar?
    Quote Originally Posted by Salem View Post
    You've already lost if you're doing financial calculations in floating point.
    To expand on what Salem and whiteflags are hinting at; there is no professional financial software in the world that uses any type of floating point calculations to handle transactions. They all deal the the smallest possible representation of the number system in whole amounts. This is due to the inherent inaccuracy of floating point calculations. So, for example if you were to be handling American currency, all your calculations should be done in whole number amounts of cents. So, $5 would be 500 pennies.

    Note: This example is still a gross simplification of how financial software actually operates but this should be sufficient enough for what you are trying to do.

    EDIT: Read: What every computer scientist should know about floating point arithmetic
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    there is no professional financial software in the world that uses any type of floating point calculations to handle transactions.
    I wouldn't go that far... I'm betting that right now there are a dozen shiny new Turbo C programmers, still a little moist behind the ears, out there pounding out the best-EVER accounting package just loaded to the brim with floating point math and sequential files loaded into linked lists....

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Prat View Post
    Hello Everyone,

    I searched on internet but could not find the answer to meet my requirement, I am facing the Conversion problem from double to long without truncation.

    In my VC++ application there are some complex tax calculation logic's, and we have to do calculation for all double values and assign those to long type without any truncation.

    So, please help me, if having some way for value conversion from double to long in C language ?


    Thanking You.
    Prat.
    Prat... let me add my vote to those already cast... If you are handling money... use 64bit integers and work in minimum monteary units (usually pennies) right from square 1...
    Last edited by CommonTater; 08-21-2011 at 10:47 AM.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    For starters, find out whether sizeof(double) == sizeof(long) in VC++.
    If it's not then follow what Salem and others have noted in their posts.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    I wouldn't go that far... I'm betting that right now there are a dozen shiny new Turbo C programmers, still a little moist behind the ears, out there pounding out the best-EVER accounting package just loaded to the brim with floating point math and sequential files loaded into linked lists....

    LOL......that is probably true. Time to bust out that "Made in India" stamp.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. number bigger than long long double
    By suryak in forum C Programming
    Replies: 9
    Last Post: 08-18-2011, 02:02 PM
  2. Replies: 1
    Last Post: 04-23-2011, 08:40 PM
  3. Replies: 1
    Last Post: 10-11-2010, 01:53 AM
  4. Replace double with long double in all my code.
    By boyfarrell in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 04:17 PM
  5. How to convert type string to char/double ?
    By Hermitsky in forum C++ Programming
    Replies: 6
    Last Post: 07-08-2004, 03:25 PM