Go Down

Topic: How to truncate a float value (Read 24437 times) previous topic - next topic


What is the correct syntax to truncate a float value?  For instance, if I have a float value of 3.1456987456321156 and I want it to actually be 3.14 -- what is the syntax in Arduino for that?


If you want to format your number as output, sprintf is the usual thing to use. Have a google on setting width and precision in printf or sprintf.

I can't imagine any reason to truncate a floating point number in a calculation, but if you want to do it, something like this for a floating point  value f should get you close  

((int)(f * 100 ))/ 100.0


Well, I know that floating points are difficult to use because they carry out to so many decimal points that comparing two numbers for similarity will almost certainly result in the numbers not being the same.  If I can truncate it down to only two decimal places, that will be enough to determine if two results are the same or not.  After three or four decimal places, my numbers will start to change, but I dont care about that much detail for my project.  

I am converting analog readings (0-1052)  to velocity readings which will only change slightly and result in fractions or rather decimals.  So, I want to use float, but only carry to two decimal places.  I'm just not familiar with C/C++ and it's syntax.


Feb 13, 2008, 04:39 pm Last Edit: Feb 13, 2008, 04:43 pm by mem Reason: 1
if you want to see if two floating point number are the same up to the first two decimal places you could call a function like this:

Code: [Select]
boolean isEqual(float f1, float f2){
 return ( (int)(f1 *100)) == ((int)(f2 * 100) );

this should return true if the two floats passed are the same up the first two decimal places. If your floating point numbers will be bigger than the largest signed integer divided by 100  (i.e. 327 ) then change the cast of (int) to (long) in both places in the function

There may actually be a C library function to do this but I avoid using floats if I can, so I have not needed to look for one


Feb 13, 2008, 05:01 pm Last Edit: Feb 13, 2008, 05:07 pm by mem Reason: 1
BTW, You can achieve the same thing more efficiently by doing your entire calculation using integers (or longs) and multiplying the numerator by 100 before you do any division.

i.e. to calculate     velocity =  distance  / time

where distance =  51
and       time =  31

the floating point result is 1.64516?

but if you use just integers (or longs) and multiply the numerator by 100 you get:

   5100 / 31 =  164

Which is the velocity rounded off to two decimal places times 100.  This is much more efficiently calculated by the arduino hardware, uses less memory and is easier to compare than the floating point  version.

Your logic just needs to take into account that your results are not in (for example) meters per second but in centimetres per second.


There's a little confusion here:

Truncating usually means removing EVERYTHING after the decimal point.

What you want (as far as i can see) is to round your float to 2 decimals.


Feb 14, 2008, 02:12 pm Last Edit: Feb 14, 2008, 02:21 pm by mem Reason: 1
because the ADC has only 10 bits of precision it probably doesn't make any difference if the comparison is done on a number rounded before truncating or not. But if it does then .005 could be added to the floats before truncating.


MikMo -- you're right.....I did mean to round off.
Mem -- thanks for the insight.  I think I can make your example work.  At least it makes sense in my head.  I'll have to see if i can put it on paper now.

Thanks again.

Go Up