hallo
i need 2 deal with each digit after the floating point.
ex . 12.5678
i need 2 compare the 12.5 with another number and then the 12.56 and then 12.567 and so on till 4 digits

is there is a function that takes the int number and one number after the floating point from the whole float number

Dani AI

Generated

Short answer: scale to an integer and compare/extract digits from that integer. 's quick split (integer part = cast, fract = a - int) is a useful start, but it can mislead because binary floats don't store decimal fractions exactly and casting truncates toward zero (so negatives are tricky). Multiplying a fractional float by 10, 100, ... can produce values like 12.559999 instead of 12.56.

Recommended, robust approach (rounding or truncation):

  • Decide whether you want rounding or truncation.
  • Compute p = 10^n.
  • For rounding: scaled = llround(x p). For truncation: scaled = (long long)floor(x p) (handle negatives with floor).
  • Compare scaled values for two numbers; equality means they match up to n decimals.
  • To extract digits, take frac = scaled % p and pull digits with integer division / modulo.

Example implementation (C99):

#include <math.h>
#include <stdlib.h>

/* integer 10^n */
static long long pow10_int(int n) {
    long long v = 1;
    while (n-- > 0) v *= 10;
    return v;
}

/* compare a and b up to n fractional digits (uses rounding) */
int same_up_to(double a, double b, int n) {
    long long p = pow10_int(n);
    long long sa = llround(a * p);
    long long sb = llround(b * p);
    return sa == sb;
}

/* fill digits[0..n-1] with fractional digits (most-significant first) */
void extract_digits(double x, int n, int digits[]) {
    long long p = pow10_int(n);
    long long s = llround(fabs(x) * p);
    long long frac = s % p;
    long long div = p / 10;
    for (int i = 0; i < n; ++i) {
        digits[i] = (int)((frac / div) % 10);
        frac %= div;
        div /= 10;
    }
}

Practical notes:

  • Use double (not float) for fewer surprises.
  • Rounding can create carries (1.9995 with n=3 may round to 2.000). If you need pure truncation, use floor-based logic.
  • Watch overflow: keep n small so that p * |value| fits in 64 bits.
  • For exact decimal needs (money), prefer integer fixed-point (store cents) or a decimal library rather than binary floats.

You can get the parts by doing this

float a=12.5678;
int whole_part=a;
float dec_part=a-whole_part;

If you want only one decimal part try this

float dec_part_1=((int)(dec_part*10))/10;

If you want two, replace the 10 with 100 and so on.

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.