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

Dani AI

Generated

Several good suggestions already appear: mentioned casting, described the multiply/floor trick, pointed to splitting with modf, and showed a quick Arduino-style cast. The main practical pitfalls to watch for are: the C++ % operator is integer-only, naive casts truncate (not round), binary floating-point can produce tiny errors (2.33 may be stored as 2.3299999999...), and rounding can carry (0.9999 -> 1.00). The snippet below is a compact, safe C++11 approach that (1) splits integer/fraction with modf, (2) rounds the fractional part to two decimals with llround, and (3) handles the carry and negatives.

#include <cmath>
#include <utility>

std::pair<long long,int> splitToTwoDecimals(double x) {
    bool neg = x < 0.0;
    x = std::fabs(x);

    double ipart;
    double frac = std::modf(x, &ipart);          // frac in [0,1)
    long long cents = static_cast<long long>(std::llround(frac * 100.0));
    if (cents == 100) { ipart += 1.0; cents = 0; } // handle 0.9999 -> 1.00
    long long whole = static_cast<long long>(ipart);
    if (neg) whole = -whole;
    return std::make_pair(whole, static_cast<int>(cents));
}

Usage notes: the function returns (whole, cents) where cents is 0..99 and whole keeps the sign. Reconstruct carefully: if whole >= 0 then value = whole + cents/100.0, otherwise value = whole - cents/100.0. Examples: splitToTwoDecimals(2.33333) -> (2, 33); splitToTwoDecimals(2.9999) -> (3, 0); splitToTwoDecimals(-2.335) -> (-2, 34).

For anything monetary or high-accuracy, avoid double for storage: store cents as integers (or use a decimal/fixed-point library). Also pick the rounding rule you need: std::llround is round-half-away-from-zero; if you require banker's rounding (round-to-even) use the appropriate rounding-mode functions or implement the tie-breaking rule explicitly.

Recommended Answers

All 4 Replies

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

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 
 *  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.