STR$ Function missing leading spaces

Here we can discuss about the problem found
Post Reply
jpd8266
Posts: 3
Joined: Sat Oct 21, 2023 7:03 am

STR$ Function missing leading spaces

Post by jpd8266 »

Hi,
I am trying to align data on various display ( LCD 4x20 I2C, OLED Display, etc), using the STR$ function as per the example in Annex Help.
(Firmware Annex32 CAN 1.51.5 LFS on ESP32)

Code: [Local Link Removed for Guests]

''Test STR$Function as per Annex Help
'Missing leading spaces when using %x format

  a=12.34567890
  wlog "Value=";STR$(a,"%2.4f") 'should print Value=12.3457, - (note also the rounding on decimals)
  wlog "Value=";STR$(a,"%3.5f") 'should print Value= 12.34568
The number after the % sign parameter has no effect of the output.
Where is the problem ?

Thank you for your advices,
JP
Last edited by jpd8266 on Fri Nov 24, 2023 1:15 pm, edited 1 time in total.
User avatar
cicciocb
Site Admin
Posts: 1989
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 426 times
Been thanked: 1329 times
Contact:

Re: STR$ Function missing leading spaces

Post by cicciocb »

Honestly I don't clearly understand your question.
Can you rephrase again ?

Probably what you say : "The X parameter has no effect of the output." means that you want show the number in hex format ?

If this is your question, probably you must do the following

Code: [Local Link Removed for Guests]

a = 129
wlog STR$(a, "%04x", 1)  ' if you want leading zeros '0'
wlog STR$(a, "%4x", 1)  ' if you want leading spaces
jpd8266
Posts: 3
Joined: Sat Oct 21, 2023 7:03 am

Re: STR$ Function missing leading spaces

Post by jpd8266 »

Sorry, my mistake :oops: :oops: : I will edit my original post, and will get new eyeglasses!

I meant the number after the % sign:

%2.4f to get 2 digits before decimal point
%3.4f to get 3 digits (or space) before the decimal point.

All this to vertically align values on a multilines display.
User avatar
cicciocb
Site Admin
Posts: 1989
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 426 times
Been thanked: 1329 times
Contact:

Re: STR$ Function missing leading spaces

Post by cicciocb »

Yes, you are right, there is an error in the help file

The correct code is the following :

Code: [Local Link Removed for Guests]

a=12.34567890
wlog STR$(a, "%7.4f")
wlog STR$(a, "%9.5f")
In fact, the first number (i.e. 7 in %7.4f) represents the total number of characters to print, including the decimal point.
I''m going to update the help file.

You can find this information on the printf "c" function on internet.
jpd8266
Posts: 3
Joined: Sat Oct 21, 2023 7:03 am

Re: STR$ Function missing leading spaces

Post by jpd8266 »

Thank you ciccocb,

It works fine !
JP
Post Reply