FloatToStr

Post your requests and ideas on the future development of mikroBasic PRO for PIC.
Post Reply
Author
Message
microchip22
Posts: 41
Joined: 31 Mar 2012 19:13

FloatToStr

#1 Post by microchip22 » 19 Jul 2015 01:44

The FloatToStr conversion needs to be modified so that it will round to a set number of decimal points.

User avatar
aleksa.jovanovic
Posts: 526
Joined: 30 Jun 2015 08:48

Re: FloatToStr

#2 Post by aleksa.jovanovic » 20 Jul 2015 09:40

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

Post Reply

Return to “mikroBasic PRO for PIC Wish List”