Float to string convertion?

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Float to string convertion?

Post by fotios »

Hi
How could we convert a non-integer (float) with 1 or 2 digits in fractional part to string? What i need is to print e.g. the number 17.4 or 17.45 on the LCD. This process is included in the ADC component, e.g. you could select "ReadAsString" and the non-integer value will be printed with two fractional digits on the LCD, e.g. 2.00 or 2.25 etc. How can we apply the same process in other numbers? Practically speaking, i have two floating variables: Voltage = 225V and Current = 17.5A. So (again floating variable) Power = 225 X 17.5 = 3937.5W. Voltage and Current are continuously changed, and accordingly the Power. These 3 values should be updated every second on LCD display.
Thanks
Best Regards FOTIS ANAGNOSTOU

User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

Re: Float to string convertion?

Post by tiny »

I hope I have understood correctly, you are looking for a way to display analog values as a string and display them on a display.
The easiest way is via "GetVoltage", here you can easily adjust the range.
Here is a small example of voltage, current and power.
Christina
Float to String.fcfx
(14.24 KiB) Downloaded 259 times

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: Float to string convertion?

Post by fotios »

Hi Christina
Yes, you got me. For mains voltage reading (230Vac nominal) actually i need only the integer part for the LCD, the fractional part will be discarded. For current reading i need the fractional part for LCD but up to 1 digit, e.g. if is 12.43 i only need 12.4. For Power indication i need only the integer part for the LCD again. There is also the Energy consumption value (KWh) which must be indicated on LCD and will be updated each second, i.e. E=P*time/3600Wsec. Actual energy counters contain 6 digits, the last is fractional e.g. 12345.6 and when 99999.9 is reached, the counter is again started from 00000.0. Summarizing, i need one fractional digit for the current indication and one fractional digit for the energy indication.
Best wishes for a Happy New Year :D
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: Float to string convertion?

Post by fotios »

OK, i found the way to discard the fraction part of voltage and power, and to have only one fraction digit for current indication:

Code: Select all

Str1 = FloatToString$ (f1,0)

Code: Select all

Str2 = FloatToString$ (f2,1)

Code: Select all

f3 = f1 * f2
Str3 = FloatToString$ (f3,0)
It remains to verify on actual hardware.
Many thanks again
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: Float to string convertion?

Post by fotios »

And yes, it works on actual hardware
Best Regards FOTIS ANAGNOSTOU

User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

Re: Float to string convertion?

Post by tiny »

I am glad that I could help you!
Christina

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: Float to string convertion?

Post by fotios »

Happy 2018 :D

Now i have to implement an electric energy consumption counter in KWh, similar to this installed in our home by the electric company which can count up to 99999.9 KWh and then rolls over to 00000.0. It increments by 0.1KWh.
Let's make some calculations: A 3600W load connected for 1 hour to power grid consumes 3600Wh or 3.6KWh. It consumes 3600/60 = 60W per min and 3600/3600 = 1W per sec and let's say it 1Ws. How can we increment by 1 a PIC variable each sec? A first thought (maybe silly) is to use the TMR1 16bit module. It counts up to 65535 (FFFF), rolls over to 0 and sets the TMR1IF interrupt flag. If we use the PIC Fosc=4MHz as source and prescaler 1:1 the TMR1 will increment by 1 per: 4MHz/4=1MHz=>1/1MHz=1usec. So after 65535us plus 2 machine cycles, i.e. 65537us will overflow. If we load the TMR1 with an offset of 65537-50000=15537 will overflow each 50000us and if increments a variable each time, this variable will reach 20 exactly after 1sec.
Any better idea is welcomed. Provided that only internal modules of PIC will be used to keep a minimal hardware. Later we could include a suitable xtal as clock source for TMR1.
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: Float to string convertion?

Post by fotios »

Why i thought TMR1? Because the load will vary. For example, it will start from 4KW and gradually will drop to 0.5KW. A 4KW load will consume 4000/3600=1.1111W per second or 1/1.1111=0.9 i.e. 1W per 0.9sec. Now we have to change the offset so as TMR1 will overflow each 0.9sec. So, 65535*0.9=58981.5 => 65537-58981=6556 new offset value. Let's make a calculation for 1800W: 1800/3600=0.5Ws or 1/0.5=2sec i.e. 1W per 2sec. For 900W load, the consumption is 1W per 4sec and so on. Hmmm... :?
Best Regards FOTIS ANAGNOSTOU

User avatar
AbhijitR
Posts: 299
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 280 times
Been thanked: 79 times
Contact:

Re: Float to string convertion?

Post by AbhijitR »

Hello! Christina

Thanks for the example, it was rather very easy to use the "GetVoltage" option, never had a thought of using that before.

Regards
Abhi

Post Reply