12,360,642 members (56,528 online)
Rate this:
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 19-Dec-12 4:20am
merh524
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:

## 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

Rate this:

## 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.
v2
Rate this:

## 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:

## Solution 5

hi dear friend

```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;
```

Top Experts
Last 24hrsThis month
 Richard MacCutchan 310 OriginalGriff 183 Dave Kreskowiak 180 RyanDev 115 F-ES Sitecore 85
 Richard MacCutchan 320 OriginalGriff 233 Dave Kreskowiak 205 Karthik Bangalore 180 RyanDev 115