Truncating or rounding displayed variables
11 year
Hi ,
I am measuring small objects under a microscope with RR, and displaying the results with wither the 'display Text' function or 'display variables' functions.

The Variables are being manipulated in the C interpretor and are written out with the setFloatVariable command.

Is there an easy way of truncating or removing decimal places from the displayed variables (without turning them to 'int' alltogether).

eg to display 45.323131  as 45.32?
many thanks,
Steven Gentner from United States  [1446 posts] 11 year

Yes, if you use something like

x = (float)((int)(x*100.0f))/100.0f;

that will round the number to 2 decimal places. Is that what you need?

Anonymous 11 year
Hi Steven,
THanks for your reply.. That did something....
I had to remove the 'f's after the 100.0 to get it to run.

It now displays the number as follows... 45.320000

but sometimes as 45.320001 or 45.319999.

It would be nice to be able to remove the extra zeros...
Steven Gentner from United States  [1446 posts] 11 year

You can force it into a string to specify exactly how many decimal points you see. Assuming the CScript module use

float x = 5.12345;

x = ((int)(x*100.0))/100.0;

char buffer[32];
sprintf(buffer, "%f", x);
setStrVariable("x", buffer);

to force it to display X as a string instead of a float.


This forum thread has been closed due to inactivity (more than 4 months) or number of replies (more than 50 messages). Please start a New Post and enter a new forum thread with the appropriate title.

 New Post   Forum Index