none
Numeric to text conversions RRS feed

  • Question

  • So far, I have been unable to discover how to convert a numeric value to text. The program (the unfinished program has been posted) requires a numeric input of a dollar amount. I need to format that output to look like $###,###.## so that it can be placed onscreen with the decimals lined up and right justified. Suggestions?

    Thanks....

    Monday, January 21, 2013 10:34 AM

Answers

  • SmallBasic doesn't have numeric values, all variables are strings. The mathematical functions (e.g. add or multiply) treat string as numeric values if possible. If you want to output a (numeric) string in a certain format you have to convert it yourself. The following program does your conversion. The only drawback is that values smaller than 1000 still prints with a comma.
    val=12345.2
    
    v1=math.Floor(val)
    v9=math.Floor(100*(val-v1))
    v1="x         "+v1
    lv=text.GetLength(v1)
    v2="$"+text.GetSubText(v1,lv-5,3)+","+text.GetSubTextToEnd(v1,lv-2)
    v2=v2+"."+v9
    TextWindow.WriteLine(v2)
    



    Jan [ WhTurner ] The Netherlands

    • Marked as answer by elmo533940 Monday, January 21, 2013 12:55 PM
    Monday, January 21, 2013 12:03 PM
    Answerer

All replies

  • SmallBasic doesn't have numeric values, all variables are strings. The mathematical functions (e.g. add or multiply) treat string as numeric values if possible. If you want to output a (numeric) string in a certain format you have to convert it yourself. The following program does your conversion. The only drawback is that values smaller than 1000 still prints with a comma.
    val=12345.2
    
    v1=math.Floor(val)
    v9=math.Floor(100*(val-v1))
    v1="x         "+v1
    lv=text.GetLength(v1)
    v2="$"+text.GetSubText(v1,lv-5,3)+","+text.GetSubTextToEnd(v1,lv-2)
    v2=v2+"."+v9
    TextWindow.WriteLine(v2)
    



    Jan [ WhTurner ] The Netherlands

    • Marked as answer by elmo533940 Monday, January 21, 2013 12:55 PM
    Monday, January 21, 2013 12:03 PM
    Answerer
  • What I was looking for was the equivalent of the format template $##,###,00. the following is the solution that I worked out.

    Label = 1030
    DollarFormat()
    Credit = Label
    TextWindow.WriteLine(Credit)

    Sub DollarFormat
      Pos = Text.GetIndexOf(Label, ".")
      str = Text.GetSubTextToEnd(Label, Pos + 1)
      Len = Text.GetLength(str)
      If Len = 1 Then
        Label = TextPlus.Append(Label, "0")
      ElseIf Pos = 0 Then
        Label = Text.Append(Label, ".00")
      EndIf
      Label = "$" + Label
      While Text.GetLength(Label) < 10
        Label = " " + Label
      EndWhile
    EndSub

    Monday, January 21, 2013 6:44 PM