1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Formatting issue with number

Discussion in 'Oshonsoft' started by jnilo, Mar 18, 2015.

  1. jnilo

    jnilo New Member

    Joined:
    Mar 15, 2015
    Messages:
    9
    Likes:
    0
    I have two numbers that can vary between 1 and 22. Whenever any of the two is comprised between 1 and 9 I would like to pad them with a zero e.g.
    01/22 or 01/09 so that my result is always displayed at the same place. I cannot find a num to char conversion function. Any trick to suggest ? (I could have four different lcdout statements according to the different values of my two numbers but it seems a bit cumbersome...)
    Thanks
    Jacques
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,145
    Likes:
    907
    Location:
    Rochdale UK
    Oshonsoft doesn't cover any formatting of that nature... You need your own integer to ASCII routine ..

    Its dead simple..

    Assume the number is always 4 digits long ( if not you will have to adjust )
    What you are looking for is right align!!

    Here is mine
    Code (basic):

    Sub PrintNumb( value as word)
        dim temp as word
        temp = value / 1000
        Lcdout #temp
        temp = ( value /  100 ) MOD 10
        Lcdout #temp
        temp = (value / 10 ) MOD 10
        Lcdout #temp
        temp =  value  MOD 10
        Lcdout #temp
    End Sub
     
    I haven't tested this... Its just so you get the idea!!
     
    • Like Like x 1
  3. jnilo

    jnilo New Member

    Joined:
    Mar 15, 2015
    Messages:
    9
    Likes:
    0
    Nice trick ! The correct code for oshonsoft using sub is:
    Code (basic):

    printnumb:
      Dim temp As word
      temp = value / 1000
      Lcdout #temp
      temp = (value / 100) Mod 10
      Lcdout #temp
      temp = (ii / 10) Mod 10
      Lcdout #temp
      temp = ii Mod 10
      Lcdout #temp
    Return
     
    Thanks !
    Jacques
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,145
    Likes:
    907
    Location:
    Rochdale UK

    Sorry! I have the addin for structured programming... I can use Sub's and Functions in Oshonsoft..

    Using Sub allows you to pass a variable rather than have it global!!!
     
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,174
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    Look under Oshonsoft snippets
    http://www.electro-tech-online.com/threads/code-snippets-for-the-oshonsoft-basic-compiler.112540/


    Code (text):

    Dim ascbfr3 As Byte  'used in convert routing
    Dim ascbfr2 As Byte  'used in convert routing
    Dim ascbfr1 As Byte  'used in convert routine
    Dim ascbfr0 As Byte  'used in convert routine

    binval = volts
    Gosub bin2asc

    bin2asc:
    ascbfr3 = binval / 1000
    temp3 = binval Mod 1000
    ascbfr2 = temp3 / 100
    temp3 = temp3 Mod 100
    ascbfr1 = temp3 / 10
    ascbfr0 = temp3 Mod 10
    'results are BCD so
    'convert to ASCII for LCD
    ascbfr3 = ascbfr3 Or 0x30
    ascbfr2 = ascbfr2 Or 0x30
    ascbfr1 = ascbfr1 Or 0x30
    ascbfr0 = ascbfr0 Or 0

    ' and to show on a lcd after it is set up

    Lcdout "Volts:", ascbfr3, ascbfr2, ".", ascbfr1, ascbfr0

    'this will put a decimal point in and the example shows a voltage reading to 2 decimal places
     
     
    • Like Like x 1
  7. jnilo

    jnilo New Member

    Joined:
    Mar 15, 2015
    Messages:
    9
    Likes:
    0
    Ian, Eric: thanks to both of you. In my case the following code works perfectly, j1 and j2 being the two bytes variables comprised between 1 and 22. It seems that I do not need the or x 30 mask. All variables are declared as bytes.
    Jacques
     

Share This Page