# ofxGui float slider precision

Hello,

I am using an ofxGui float slider that is updated with a float OSC received value.
This value is normalized as a float betwenn 0 and 1.

The slider displays a 6 decimal precision digit (ie : 0.234112)

is there a way to limit the float precision to only 2 decimal for exemple ?

thank you

``````float numWithLongDec = 3.14159;
float numWithTwoDec = static_cast<int> (numWithLongDec * 100 ) / 100.0;
float numWithThreeDec = static_cast<int> (numWithLongDec  * 1000 ) / 1000.0;

ofLog(OF_LOG_NOTICE) << "Original number: " << numWithLongDec; // 3.14159
ofLog(OF_LOG_NOTICE) << "Two decimal places: " << numWithTwoDec; // 3.14
ofLog(OF_LOG_NOTICE) << "Three decimal places: " << numWithThreeDec; // 3.141
``````

What is happening here? When you cast an Float to an Int the number is truncated. So 3.14159265359 * 100 = 314.159, truncated is 314. Then when you divide the number by 100 and store the value into the float, you get your two decimal places: 3.14.

Similarly for three decimal places you multiply then divide by 1000. Keep on adding more zeros for more decimal places.

``````float numWithLongDec = 3.14159;
float numWithTwoDec = static_cast<int> (numWithLongDec * 100 ) / 100.0;
float numWithThreeDec = static_cast<int> (numWithLongDec  * 1000 ) / 1000.0;

ofLog(OF_LOG_NOTICE) << "Original number: " << numWithLongDec; // 3.14159
ofLog(OF_LOG_NOTICE) << "Two decimal places: " << numWithTwoDec; // 3.14
ofLog(OF_LOG_NOTICE) << "Three decimal places: " << numWithThreeDec; // 3.141
``````

What is happening here? When you cast an Float to an Int the number is truncated. So 3.14159 * 100 = 314.159, truncated is 314. Then when you divide the number by 100 and store the value into the float, you get your two decimal places: 3.14.

Similarly for three decimal places you multiply then divide by 1000. Keep on adding more zeros for more decimal places.

thanks for the comment !

ok, it seems logical So there is no method like truncate() or something ?

thanks again

1 Like

yes you can use:

``````std::cout << std::fixed << std::setprecission(2) << floatNumber << std::endl;
``````

or:

``````std::cout << ofToString(floatNumber,2) << std::endl;
``````

where floatNumber is the number you want to print. but there’s nothing like that for ofxSlider yet

ok thanks a lot.

So if i want to show only 0.213 in ofxSlider Gui instead of 0.213423 it is not possible for now ?

not a big deal, but there is no need for such accuracy

thanks

It’s not a big deal to make it. You just have to make your own function like the following as @veryphatic proposed:

``````#include <math.h>
float truncate(float _input, int _precision){
if(_precision<0)
_precision = 0;
float offset = powf(10, _precision);
return std::static_cast<int>(_input * offset) / offset;
}
``````

and then you can use it:

``````float my_float_value(3.14159);
my_float_value = truncate(my_float_value, 4); ///> gives 3.1415
my_float_value = truncate(my_float_value, 2); ///> gives 3.14
my_float_value = truncate(my_float_value, 1); ///> gives 3.1
my_float_value = truncate(my_float_value, 0); ///> gives 3.0
``````

done ?

1 Like

I was just wondering if there were any existing method i may have missed, but yeah, sure if not i will make my own !

Methods suggested here are doing the job.

Thank you.