954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to separate decimal from 2.33333

I am trying to make a division and with the answer I want to separate the decimal and round it to two decimal points.
After this, I want to assign the decimal part to another function.
Thank You

Mexkn
Newbie Poster
1 post since May 2009
Reputation Points: 10
Solved Threads: 0
 

I think you should check how %-operator and casting a float into an integer work :)

Topi Ojala
Junior Poster in Training
66 posts since May 2009
Reputation Points: 13
Solved Threads: 20
 

These threads might be useful.

http://www.daniweb.com/forums/thread184830.html
http://www.cplusplus.com/reference/clibrary/cmath/floor/
http://www.cplusplus.com/reference/clibrary/cmath/pow/

To round down to the lower tenth, multiply the decimal part by 10, take the floor, then divide by 10.

5.6789 // original number.
.6789 // decimal part.
6.789 // decimal part times 10
6.0 // floor it
0.6 // divide by 10.

That rounds down. To round off you'll need to add 0.05 before flooring.

It's the same concept for the nearest hundredth, thousandth, etc. See the pow link to get the powers of 10.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

May be it helps (see modf function: split integer and fractional parts):

/**
 *  2008-10-01 Beta version. No warranties...
 *  Rounding functions freeware mini-package.
 *  About rounding forms and MS rounding stuff
 *  see http://support.microsoft.com/kb/196652
 *  Dependencies: <math.h> <float.h>
 *  Languages: standard C and C++
 *  Not optimized!..
 */
/** Not-optimized 10 power n (n > 0) */
static double tenpow(int n) {
    double y = 10.0;
    while (--n > 0)
        y *= 10.0;
    return y;
}
/** 64-bit double precision ~ 15 digits. */
static int nMax = DBL_DIG; /* from float.h */

/** Round off with Banker's method, n in -15..15 */
double Round(double x, int n) {
    bool neg = (x < 0.0);
    double ipart, fpart;
    double y, p;
    int id;
    if (neg)
        x = -x;
    if (n > 0) {
        double yy;
        fpart = modf(x,&ipart);
        if (n > nMax)
            n = nMax;
        p = tenpow(n);
        y = fpart * p;
        fpart = modf(y,&yy);
        if (fpart < 0.5)
            fpart = 0.0;
        else if (fpart > 0.5)
            fpart = 1.0;
        else { /* Banker's Method */
            id = (int)fmod(yy,10.0);
            fpart = (id&1)? 1.0: 0.0;
        }
        yy += fpart;
        y = ipart + yy / p;
    }
    else if (n < 0) {
        if (n < nMax)
            n = -nMax;
        p = tenpow(-n);
        y = x / p;
        y = Round(y,0) * p;
    }
    else { /* n == 0 */
        fpart = modf(x,&ipart);
        if (fpart > 0.5)
            ipart += 1.0;
        else if (fpart < 0.5)
            ;
        else { /* Banker's Method */
            id = (int)fmod(ipart,10.0);
            if ((id&1) != 0)
                ipart += 1.0;
        }
        y = ipart;
    }
    return neg?-y:y;
}
/** Symmetric arithmetic rounding, n in -15..15 */
double round(double x, int n) {
    bool neg = (x < 0.0);
    double ipart, fpart;
    double y, p;
    int id;
    if (neg)
        x = -x;
    if (n > 0) {
        double yy;
        fpart = modf(x,&ipart);
        if (n > nMax)
            n = nMax;
        p = tenpow(n);
        y = fpart * p;
        fpart = modf(y,&yy);
        if (fpart < 0.5)
            yy += 1.0;
        y = ipart + yy / p;
    }
    else if (n < 0) {
        if (n < -nMax)
            n = -nMax;
        p = tenpow(-n);
        y = x / p;
        y = round(y,0) * p;
    }
    else { /* n == 0 */
        fpart = modf(x,&ipart);
        y = (fpart < 0.5)? ipart: ipart + 1;
    }
    return neg?-y:y;
}
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
float x = 12.34;
 int y = x;
 int z=(x-y)*100;
 Serial.println("XXXXXXXXXX TESTS XXXXXXXX");
 Serial.println(x);
 Serial.println(y);
 Serial.println(z);


This gives you
x=12.34
y=12
z=34

Easy :)

mariorenato
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You