[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
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
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.
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
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.
Re: padding zeros on strings
Quote:
Originally Posted by
Shaggy Hiker
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
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.
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
Re: padding zeros on strings
Thanks very much guys for your input.
techgnome's solution worked a treat thanks