Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

Thanks
M.H
Posted
Comments
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.

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:

C++
// 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.
 
Share this answer
 
Comments
Albert Holguin 20-Dec-12 18:02pm    
Good answer... +5
C++
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. :)
 
Share this answer
 
v2
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.
 
Share this answer
 
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;
t=f;
df=f-t;
t1=df*1000000;
while(!t1%10)
   t1/=10;

t1/=10;      //remove last digit
df=t1/(float)1000000;
f=t+df;
//f is your number
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900