PDA

View Full Version : Formatting numbers & strings



dco045
26-01-2018, 01:46
Hi,

I am lookig for a way to format numbers as financial format ex : 123 456 789.23
I tryed
PrintL Format$(123456789.23, "000 000 000.00") result OK : 123 456 789.23
but - - - - - - - - 123456 - - - - - - result : 000 123 456.00 - - - - - - - - wished ~~~ 123 456.00 ~ stands here for spaces.

I also wish to print strings, whose length vary from 1 to 16 bytes in hexadecimal

as an example "ErosOlmi" would print as 45626F634F6C6D69

Regards


Dany going to sleep.....

ErosOlmi
26-01-2018, 02:08
For numeric use # instead of 0 in formatting string

For string to hex ... I've nothing at the moment if I remember well.
But you can create a function


uses "Console"
PrintL Format$(123456789.23, "### ### ###.##")
PrintL Format$(12345.23 , "### ### ###.00")
printl StringToHex("ErosOlmi", 16)
waitkey


function StringToHex(s as string, optional lMaxLen as long) as string
String sBuffer
for i as long = 1 to len(s)
sBuffer += hex$(asc(s, i), 2)
next
function = left$(sBuffer, lMaxLen)
end function


Ok, now I'm going to have some sleep :zzz:
because tomorrow will be an hard working day :badday:
Ciao

dco045
11-02-2018, 21:45
HI Eros,



For string to hex ... I've nothing at the moment if I remember well.
But you can create a function


Function used OK , perhaps could be added as standard string function ?



But numbers formatting still causes trouble.




For numeric use # instead of 0 in formatting string

PrintL Format$(123456789.23, "### ### ###.##")
PrintL Format$(12345.23 , "### ### ###.00")

As you can see in attachments, the second line does'nt insert leading spaces.

And the last part of script should display numbers as right aligned.
In the examples tilde char '~' is here to represent spaces.

Best regards.


Dany under the snow :D

ErosOlmi
12-02-2018, 17:55
Ciao,

Format$ has not been developed to align data but only format data to a numeric format.
"0" means a fixed place. "#" mean a variable place not substituted by space.

To have alignment, you need to mix Format$ with RSet$ (http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?rset$.htm)

Like in the following Code:

Uses "Console"


long nSpace = 7


PrintL RSet$(Format$(123456, "#0"), nSpace) & " expected 123456"
PrintL RSet$(Format$(12345 , "#0"), nSpace) & " expected ~12345"
PrintL RSet$(Format$(1234 , "#0"), nSpace) & " expected ~~1234"
PrintL RSet$(Format$(123 , "#0"), nSpace) & " expected ~~~123"
PrintL RSet$(Format$(12 , "#0"), nSpace) & " expected ~~~~12"
PrintL RSet$(Format$(1 , "#0"), nSpace) & " expected ~~~~01"
WaitKey

dco045
12-02-2018, 20:22
Ciao,

Format$ has not been developed to align data but only format data to a numeric format.
"0" means a fixed place. "#" mean a variable place not substituted by space.

To have alignment, you need to mix Format$ with RSet$ (http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?rset$.htm)

Like in the following Code:

Uses "Console"


long nSpace = 7


PrintL RSet$(Format$(123456, "#0"), nSpace) & " expected 123456"
PrintL RSet$(Format$(12345 , "#0"), nSpace) & " expected ~12345"
PrintL RSet$(Format$(1234 , "#0"), nSpace) & " expected ~~1234"
PrintL RSet$(Format$(123 , "#0"), nSpace) & " expected ~~~123"
PrintL RSet$(Format$(12 , "#0"), nSpace) & " expected ~~~~12"
PrintL RSet$(Format$(1 , "#0"), nSpace) & " expected ~~~~01"
WaitKey


Hi Eros,

Thanks, I'll write a function :escribe:



Dany

ErosOlmi
13-02-2018, 09:04
Please also have a look at USING$


Uses "Console"
PrintL USING$("###,###,###.00", 123456)
PrintL USING$("**#########.00", 12345 )
PrintL USING$("#########.00", 1234 )
PrintL USING$("#########.00", 123 )
PrintL USING$("#########.00", 12 )
PrintL USING$("#########.00", 1 )
WaitKey

dco045
13-02-2018, 18:03
Please also have a look at USING$

Hi Eros,



It's almost what I needed, it just misses the zeros left.

A format 00000#.000 --> 123.45 printed as 000123.450

:D Perhaps in ThinBasic 2.0.0.0.0.0 :D


But it's a good start for me.

Regards


Dany

ErosOlmi
13-02-2018, 19:09
Use "#" as placeholder of numbers. "0" is just "0" in USING$ function
To left fill with "0", use "*0" in front of your format. Or "**" to left fill with "*" or "*X" to left fill with "X" or "*_" to left fill with "_"
USING$ Help: http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?using$.htm

Example:

uses "Console"

printl USING$("*0#########.###", 123.45)
printl USING$("**#########.###", 123.45)
printl USING$("*X#########.###", 123.45)
printl USING$("*_#########.###", 123.45)

WaitKey

dco045
13-02-2018, 19:32
Use "#" as placeholder of numbers. "0" is just "0"
To left fill with "0", use "*0" in front of your format. Or "**" to left fill with "*" or "*X" to left fill with "X" or "*_" to left fill with "_"
USING$ Help: http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?using$.htm


Ok ,

fulfills my need

I did not flash with "*" and any_char in the help page :read: :blind:. Bang in my nose.:violent:

Can I suggest that all examples you have sent to me, should be inserted in ' USING$( )" help page ?


Right padded :D , formatted :D regards


Dany

ErosOlmi
13-02-2018, 22:22
Can I suggest that all examples you have sent to me, should be inserted in ' USING$( )" help page ?


Sure, I will

ReneMiner
01-03-2018, 07:22
Formatting Hex$?

If I remember it's quite simple:

Number x = 12345678
Long desiredLength = 16
String xHex = Hex$(x, desiredLength)

Printing xHex should show a 16-digit string now