none
input number with decimal point RRS feed

  • Question

  • This is a sample for input numbers with decimal point in a textbox. At the End the program round the number with 2 numbers after the decimal point.

    show ZKW930

    If you have a better routine, please send it.

    2012/11/26

    Uuuh, there was a mistake by numbers with no or one numbers after the decimal point.

    the corection program is ZKW930-0


    Best Regards Martin


    • Edited by martmen Monday, November 26, 2012 4:54 PM
    • Changed type martmen Monday, November 26, 2012 4:56 PM
    Saturday, November 24, 2012 2:37 PM

Answers

  • litdev, that was very clever!

    Although it's more of a SB's glitch, which doesn't remove excessive zeros after normal arithmetic operations.

    But it does remove them when using Math's category functions!

    So, just for the sake of completeness, I've also added the dot-to-comma display snippet:

    num = -162.295
    dec = 2

    Decimal()
    Commas()

    TextWindow.WriteLine("")

    Sub Decimal
     
      tmp = Math.Power(10, dec)
      num = 1/tmp * Math.Round(num * tmp)
     
    EndSub

    Sub Commas
     
      dot = Text.GetIndexOf(num, ".")
     
      If dot = 0 Then
        TextWindow.Write(num)
      Else
        TextWindow.Write( Text.GetSubText(Num  1, dot-1) )
        TextWindow.Write( "," + Text.GetSubTextToEnd(Num, dot+1) )
      EndIf
     
    EndSub

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    • Edited by GoToLoopEditor Monday, November 26, 2012 9:17 PM
    • Marked as answer by martmen Tuesday, November 27, 2012 3:33 AM
    Monday, November 26, 2012 8:59 PM
    Answerer

All replies

  • Please help me!

    Best Regards Martin

    Monday, November 26, 2012 4:58 PM
  • Hello Martin!

    Here's a very basic decimal point formatting snippet.

    Though it does not fill in zeros to right nor replaces points w/ commas. Needs more code for it!  :P

    num = 162.2883
    dec = 2
    
    Decimal()
    
    TextWindow.WriteLine(num)
    
    Sub Decimal
      
      tmp = Math.Power(10, dec)
      num = Math.Round( num * tmp ) / tmp
      
    EndSub

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Monday, November 26, 2012 7:56 PM
    Answerer
  • This puts the zeros after point
    num = 162.0
    dec = 2
    Decimal()
    TextWindow.WriteLine(num)
    Sub Decimal
      tmp = Math.Power(10, dec)
      num = 1/tmp * Math.Round( num * tmp )
    EndSub

    Monday, November 26, 2012 8:27 PM
    Moderator
  • litdev, that was very clever!

    Although it's more of a SB's glitch, which doesn't remove excessive zeros after normal arithmetic operations.

    But it does remove them when using Math's category functions!

    So, just for the sake of completeness, I've also added the dot-to-comma display snippet:

    num = -162.295
    dec = 2

    Decimal()
    Commas()

    TextWindow.WriteLine("")

    Sub Decimal
     
      tmp = Math.Power(10, dec)
      num = 1/tmp * Math.Round(num * tmp)
     
    EndSub

    Sub Commas
     
      dot = Text.GetIndexOf(num, ".")
     
      If dot = 0 Then
        TextWindow.Write(num)
      Else
        TextWindow.Write( Text.GetSubText(Num  1, dot-1) )
        TextWindow.Write( "," + Text.GetSubTextToEnd(Num, dot+1) )
      EndIf
     
    EndSub

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    • Edited by GoToLoopEditor Monday, November 26, 2012 9:17 PM
    • Marked as answer by martmen Tuesday, November 27, 2012 3:33 AM
    Monday, November 26, 2012 8:59 PM
    Answerer