Results 1 to 10 of 10

Thread: Formatting numbers & strings

  1. #1
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    65
    Posts
    37
    Rep Power
    4

    Formatting numbers & strings

    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.....
    Last edited by dco045; 26-01-2018 at 01:47. Reason: joke

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,281
    Blog Entries
    2
    Rep Power
    10
    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
    because tomorrow will be an hard working day
    Ciao
    Last edited by ErosOlmi; 26-01-2018 at 02:10.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  3. #3
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    65
    Posts
    37
    Rep Power
    4
    HI Eros,

    Quote Originally Posted by ErosOlmi View Post
    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
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	screenshot2.png 
Views:	9 
Size:	61.3 KB 
ID:	9820  
    Attached Files Attached Files

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,281
    Blog Entries
    2
    Rep Power
    10
    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$

    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
    
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  5. #5
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    65
    Posts
    37
    Rep Power
    4
    Quote Originally Posted by ErosOlmi View Post
    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$

    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


    Dany

  6. #6
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,281
    Blog Entries
    2
    Rep Power
    10
    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
    
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  7. #7
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    65
    Posts
    37
    Rep Power
    4
    Quote Originally Posted by ErosOlmi View Post
    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

    Perhaps in ThinBasic 2.0.0.0.0.0


    But it's a good start for me.

    Regards

    Dany

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,281
    Blog Entries
    2
    Rep Power
    10
    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/prod...tml?using$.htm

    Example:
    uses "Console"
    
    printl USING$("*0#########.###", 123.45)
    printl USING$("**#########.###", 123.45)
    printl USING$("*X#########.###", 123.45)
    printl USING$("*_#########.###", 123.45)
    
    WaitKey
    
    Last edited by ErosOlmi; 13-02-2018 at 19:25.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  9. #9
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    65
    Posts
    37
    Rep Power
    4
    Quote Originally Posted by ErosOlmi View Post
    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/prod...tml?using$.htm
    Ok ,

    fulfills my need

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

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


    Right padded , formatted regards


    Dany
    Last edited by dco045; 13-02-2018 at 19:33.

  10. #10
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    50
    Posts
    8,281
    Blog Entries
    2
    Rep Power
    10
    Quote Originally Posted by dco045 View Post
    Can I suggest that all examples you have sent to me, should be inserted in ' USING$( )" help page ?
    Sure, I will
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

Similar Threads

  1. prime numbers spiral
    By zak in forum Math: all about
    Replies: 4
    Last Post: 08-06-2010, 09:02
  2. Line numbers and GOTO
    By Charles Pegge in forum O2h Compiler
    Replies: 19
    Last Post: 19-04-2009, 10:44
  3. UDT.and.numbers
    By Michael Clease in forum thinBasic General
    Replies: 2
    Last Post: 07-03-2009, 00:56
  4. Source code formatting
    By ErosOlmi in forum thinAir Tips and Tricks
    Replies: 3
    Last Post: 25-09-2006, 11:55

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •