Thread: How to get the decimal part of a number...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    How to get the decimal part of a number...

    Say for example I have a double value like 237.3455. For some reason, I can't figure out how to just get the .3455 part of that number. I tried modding it by 1 (as this works for the Google calculator), but C++ doesn't like the left operand being a double for the modulus operator. Any other methods?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I'd say like

    Code:
    thatNumber - (int) thatNumber

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        double integer, value = 237.3455, fraction = modf(value, &integer);
        printf("value = %g, integer = %g, fraction = %g\n",
               value, integer, fraction);
        return 0;
    }
    
    /* my output
    value = 237.345, integer = 237, fraction = 0.3455
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    double number=237.3455

    if(number>=100 && number <=999)
    double decimalPart=((number%100)%10)%1

    else if
    etc

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    ... and if the number is on the trillions you will have a lot of ifing to do there. I don't think so...

    modf() is the perfect solution whereas Sinkula's is the most pretty. I would only remove the C style casting in favor of C++ way.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Dragoon Lover wyvern's Avatar
    Join Date
    Jul 2005
    Location
    dragooncity
    Posts
    28

    here is a good way

    try this:

    Code:
    float var1 = 123.456;
    
    int tmp = (int)var1;
    
    int var2 = var1 - tmp;
    
    if ( var2 < 0 )
    var2+1;     // cause the decimal part could be like 701 and 
                      // it will change the  tmp to 124 instead of 123

    PS:
    see this is what a real hacker does.... figures out a way of solving a problem that the others never thought off. dont misunderstand with cracker.
    http://img76.imageshack.us/img76/1808/expl7pb.png

    AN EXPLOSION DOESNT HAPPEN CUZ GOD WANTS, IT HAPPENS WHEN A SOMETHING "EXPLODES

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    On the other hand, Dave's solution is both easily understood and faster than yours. The OP didn't say he wanted a hack solution.

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    // cause the decimal part could be like 701 and
    ... and an hacker double checks his logic. A cast to int doesn't round. It truncates. Your if statement is unecessary.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by wyvern
    see this is what a real hacker does.... figures out a way of solving a problem that the others never thought off.
    Try posting something correct that solves the problem before bragging about it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    By the way, just the final nail on the coffin. I'm sure you don't mind, Wyvern. After all an hacker loves to learn new things too... It always produces 0 as the result.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Of course it does. After all, it's just Tonto's method, and then converting the decimal part gained to an int, thus guaranteeing 0 as the result. (Oh, and "var2+1" does nothing on its own. You need to assign the result.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    AN EXPLOSION DOESNT HAPPEN CUZ GOD WANTS, IT HAPPENS WHEN A SOMETHING "EXPLODES

  13. #13
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    In that case, your answer EXPLODED all over itself.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Tonto posted some psuedo-code, it wasn't his fault someone took his suggestion and crapped all over it. This thread reminds me of something from 4chan, the way we were acting.

    I hope that the OP listens to Dave and ignores the rest of us.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Number of digits in a decimal number
    By maverix in forum C Programming
    Replies: 7
    Last Post: 11-04-2007, 12:12 PM
  4. how to remove zero after decimal number
    By abhay_m8 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2007, 02:30 AM
  5. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM