728x90
반응형
WPF & C# - 제로패딩 ( ZeroPadding / PadLeft / ToString("D4") / ToString("0000") ) |
MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 | <StackPanel Margin="10" VerticalAlignment="Top"> <TextBox x:Name="tbx" Height="25" Margin="5" TextChanged="tbx_TextChanged"/> <!--Int--> <Label Margin="5" Content="int" Background="LightGray" FontWeight="Bold"></Label> <Label x:Name="lbl01" Margin="5"></Label> <Label x:Name="lbl02" Margin="5"></Label> <Label x:Name="lbl03" Margin="5"></Label> <!--String--> <Label Margin="5" Content="String" Background="LightGray" FontWeight="Bold"></Label> <Label x:Name="lbl04" Margin="5"></Label> </StackPanel> | cs |
MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | int i = 0; private void tbx_TextChanged(object sender, TextChangedEventArgs e) { if (tbx.Text != "") { i = Convert.ToInt32(tbx.Text); lbl01.Content = i.ToString().PadLeft(4); lbl02.Content = i.ToString("D4"); lbl03.Content = i.ToString("0000"); string str = i.ToString(); lbl04.Content = str.PadLeft(4); } } | cs |
제로패딩
공백을 삽입하여 자리수를 맞춰주는 역할을 한다.
입력값 1 일때
PadLeft(0) = 1 (공백 0개, 글자수(Lenght) = 1)
PadLeft(1) = 1 (공백 0개, 글자수(Lenght) = 1)
PadLeft(2) = 1 (공백 1개, 글자수(Lenght) = 2)
728x90
반응형