Results 1 to 8 of 8

Thread: [RESOLVED] padding zeros on strings

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    11

    Resolved [RESOLVED] padding zeros on strings

    Hi
    I have a numeric updown box that has one place of decimal. I find that if i set the box to 1.0 the display will be '1'. How do you pad it with leading zeroes if its between 0 and 9 and force the .0 to display after the decimal point. If is set 1.1 it shows 11 which is fine.
    any help would be great. thanks guys

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: padding zeros on strings

    works ok for me:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            NumericUpDown1.DecimalPlaces = 1
            NumericUpDown1.Increment = 0.1D
        End Sub
    
    End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    11

    Re: padding zeros on strings

    Hi paul
    Thanks for the reply. Ive got the updownnumeric being displayed in a rich textbox when i press a button Ok.
    Do i paste in that code where the Ok button code is?
    I convert the value to a string value using NumericUpDown.value.ToString before displaying it in the text box and sending it to UART.

    Quote Originally Posted by .paul. View Post
    works ok for me:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            NumericUpDown1.DecimalPlaces = 1
            NumericUpDown1.Increment = 0.1D
        End Sub
    
    End Class

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: padding zeros on strings

    When the value is displayed in a RichTextBox, then the Decimal has to be converted to a String. It's no longer a Decimal at that point, so how it displays has to do with the formatting of the string. If you are using .ToString, then you could try .ToString("N1"), which may result in what you want.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    11

    Re: padding zeros on strings

    Quote Originally Posted by Shaggy Hiker View Post
    When the value is displayed in a RichTextBox, then the Decimal has to be converted to a String. It's no longer a Decimal at that point, so how it displays has to do with the formatting of the string. If you are using .ToString, then you could try .ToString("N1"), which may result in what you want.
    Hi, thanks for the reply. The decimal point isn't really an issue. 25.5 as 255 is fine, the problem is that 9.0 showed up as 9 instead of 090

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: padding zeros on strings

    It's still an issue of string formatting. No number has a leading 0, only strings do. There are methods to pad strings (PadLeft and PadRight) which would do what you are looking for, but beyond that you'd have to show how you are taking the Decimal from the NumericUpdown control and turning it into a string to put into the RichTextBox. After all, if you have Option Strict OFF, you might be doing nothing at all. Heck, for all I know, you might not even be aware that you ARE converting a number to a string.
    My usual boring signature: Nothing

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: padding zeros on strings

    Bottom line... you pass to .ToString the format you want ... NUB.Value.ToString("00.0") so 1 will come out "01.0" and 1.9 will come out "01.9"
    or, if you need to sift the decimal out...
    (NUB.Value * 10).ToString("000")

    1 becomes "010" and 1.4 becomes "014"

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    11

    Re: padding zeros on strings

    Thanks very much guys for your input.
    techgnome's solution worked a treat thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width