Page 1 of 1

FloatToStr

Posted: 19 Jul 2015 01:44
by microchip22
The FloatToStr conversion needs to be modified so that it will round to a set number of decimal points.

Re: FloatToStr

Posted: 20 Jul 2015 09:40
by aleksa.jovanovic
Hi,

Depending on which language you are using it can be done in different ways.

For example in C it would look something like this

Code: Select all

sprintf(lux_value_string, "%5.1f", value);                  
Ltrim(&lux_value_string);    


In Basic or Pascal it would look something like this

var res : char;

Code: Select all

FloatToStr(value,lux_value_string);
res := strchr(lux_value_string, '.');
lux_value_string[res+2] := 0;
Ltrim(lux_value_string);                                                    
lux_value_string[7] := 0;
Of course this will not round the number, but just show it in a desired way (it will cut off unwanted digits).

As for the rounding, say for example that you want the number 15.68 to become 15.7, You can make an if function that will look for the second decimal character and if its higher than 5 it will increase the previous decimal by 1, if it's lower than 5 it will not. Just my thought.

I will forward the idea for the function upgrade to our software development team.

Best regards,
Aleksa