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

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

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.

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;
}
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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.