Click here to Skip to main content
12,893,190 members (55,324 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++ Windows
I have a float value like 67,800003 and the last digit can also vary. How can I remove the last digit as in this case digit 3?

Posted 19-Dec-12 4:20am
Albert Holguin 19-Dec-12 10:49am
Remove it or replace it with a certain known number (like 0)? This is important because the outcome is very different, if you remove the number as in solution 1, you'll end up with a completely different number (i.e. it will be different by a factor of ten). If you replace the number with a zero, it will only be slightly different (i.e. the number in your example will still be 67.8K).
lewax00 19-Dec-12 11:07am
In all honesty, you probably can't...not from the float itself anyways. Floats cannot represent every number, in many cases it is only an approximation. You can change how it's displayed however (like Stefan_Lang's answer below), or find a library for scientific/mathematical purposes that allows greater/arbitrary precision (I don't know of any for C/C++, so I can't help you there).
Aswin Waiba 20-Dec-12 9:23am
Will your variable only have whole numbers or would you expect real numbers too. Because the code would be different in these cases.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

That looks like a precision error to me: if you assign 67+4/5 to a float variable and print that, you'll get pretty much the same! The reason is that floats and doubles are internally converted to and stored as binary numbers which cannot accurately store a number like 4/5 anymore than in decimal you cannot accurately store 1/3 (at best you can write 0.333... , with as much digits as your storage allows).

Depending on which method you use for printing this variable, you can limit the output to less digits:

// using C-style printf
// no more than 4 digits after the decimal point, no more than 8 characters total:
printf("%8.4f\n", var); // prints "67.8000"
// using C++ style cout
// no more than 8 meaningful digits (no trailing 0s)
cout << setprecision(8) << var << endl; // prints "67.8"
// exactly 6 digits
cout <<fixed <<setprecision(6) << var << endl; // prints "67.8000"

See setprecision for more info on using cout manipulators.
Albert Holguin 20-Dec-12 18:02pm
Good answer... +5
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

bool removeLastDigit(float& number)
 int32_t tempNum = static_cast<int32_t>(number);
 tempNum = tempNum % 10;
 if( 0 == tempNum )
    return false; //No digit to remove
 number = number - tempNum;
 return true;

Won't work for real number though. :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

As other already stated, float is just an approximation - you can not represent every number accurately. The more digits you use in the integer part, the less precision you have in the fraction part of a float. When I display floating point numbers on user interfaces I usually use "%g" instead of "%f" as "%g" does rounding on the fraction part of the float and looks much nicer than "%.3f" or something similar.
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

hi dear friend
i think this code can help you:

float f=45.723;        //main digit
float df=0;   int i1=0,t=0,t1=0;
t1/=10;      //remove last digit
//f is your number

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170424.1 | Last Updated 20 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100