Results 1 to 10 of 11

Thread: Formatting numbers & strings

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    71
    Posts
    69
    Rep Power
    13

    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
    57
    Posts
    8,777
    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/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    71
    Posts
    69
    Rep Power
    13
    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 Images Attached Images
    Attached Files Attached Files

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    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/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    71
    Posts
    69
    Rep Power
    13
    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
    57
    Posts
    8,777
    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/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

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

Members who have read this thread: 1

Posting Permissions

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