Results 1 to 2 of 2

Thread: Round Up Floating Point

  1. #1

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    ok I understand.

    If you want an inline approach, this is the way:

    double y=(int)(x*100.0)/100.0;

    you power the radix by -decimal (a negative decimal gives precision lower than 1. Note the floating point division.

    for variable radix or amount of decimals you can use this generic expression:

    double z;
    int r=10,d=2;
    double y=(int)(x*(z=pow(r,d)))/z;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  2. #2
    jim mcnamara
    Guest
    Here is another way, Kedaman's is more succinct

    Code:
    double roundup2(double f){
             double tmp;
             char t[20];
            char *stopstr;	
             tmp= 5 * pow(10,-3. );
             tmp += f;
             memset(t,'\0',sizeof(t) );
             sprintf(t,"%10.2f",tmp);  // << truncate the value and put in string
             return strtod(t,&stopstr);       //  << return a float from the string
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width