Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hello again,
I was testing to truncate a value with how many decimals case i want.
Nice, i found a function that make this, but, use double, and i need to use float,
double is crashing everything with StackOverFlow errors that i can't fix.
And with float that works..
This is the code of the function.
double trunc(double value, int decimal_places){
    double integer = 0,fractional = 0,output = 0;
    int j = 0,places = 1;
    fractional = modf(value, &output);
    for( int i = 0; i < decimal_places + 1; i++ ){
        fractional = modf(fractional, &integer);
        for( j = 0; j < i; j++ ){
            places *= 10;
        }
        output += integer / places;
        places = 1;
        fractional *= 10;
    }
    return output;
}
 
Thanks
Posted 13-Dec-12 8:07am
Comments
Sergey Alexandrovich Kryukov at 13-Dec-12 13:57pm
   
If double is "crashing everything" (due to your mistakes, apparently), so you are resorting to float? Very nice. I'll tell you this: with such attitude, consider your career of a software development is ended, no sense to continue. Or rethink it.
Good luck,
--SA
Thales Maggi at 13-Dec-12 14:25pm
   
I'm out of time to stay fixing little problems, just a work to univesity, so if you don't came to help, dont sh*t my question. Thank you.
pwasser at 13-Dec-12 21:21pm
   
SA may have a point. Where is your OWN code?
Thales Maggi at 13-Dec-12 21:25pm
   
Yess! I forgot to say that! That function isn't mine! I asked help to my teacher, and he sent me a email. Obviously he found at somewhere. I found my answer, thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 309
1 BillWoodruff 300
2 Afzaal Ahmad Zeeshan 252
3 DamithSL 225
4 Maciej Los 215
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100