|
-
Feb 18th, 2016, 06:57 AM
#1
Thread Starter
New Member
[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
-
Feb 18th, 2016, 07:14 AM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 18th, 2016, 08:40 AM
#3
Thread Starter
New Member
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.
 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
-
Feb 18th, 2016, 10:44 AM
#4
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
 
-
Feb 18th, 2016, 10:56 AM
#5
Thread Starter
New Member
Re: padding zeros on strings
 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
-
Feb 18th, 2016, 10:59 AM
#6
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
 
-
Feb 18th, 2016, 11:46 AM
#7
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
-
Feb 18th, 2016, 05:27 PM
#8
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|