Convert int (number) to string with leading zeros? (4 digits) [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Number formatting: how to convert 1 to "01", 2 to "02", etc.?
How can I convert int to string using the following scheme?
1 converts to 0001
123 converts to 0123
Of course, the length of the string is dynamic. For this sample, it is:
int length = 4;
How can I convert like it?

Use String.PadLeft like this:
var result = input.ToString().PadLeft(length, '0');

Use the formatting options available to you, use the Decimal format string. It is far more flexible and requires little to no maintenance compared to direct string manipulation.
To get the string representation using at least 4 digits:
int length = 4;
int number = 50;
string asString = number.ToString("D" + length); //"0050"

Use the ToString() method - standard and custom numeric format strings. Have a look at the MSDN article How to: Pad a Number with Leading Zeros.
string text = no.ToString("0000");

val.ToString("".PadLeft(length, '0'))

Related

TextBlock string format with dot WPF [duplicate]

This question already has answers here:
Custom numeric format string to always display the sign
(6 answers)
Closed 8 years ago.
I want to format any number (integer or real) to a string representation which always has a sign (positive or negative) and a decimal separator, but no trailing zeroes.
Some samples:
3.14 => +3.14
12.00 => +12.
-78.4 => -78.4
-3.00 => -3.
Is it possible with one of the default ToString() implementations, or do I need write this myself?
Try something like this:
double x = -12.43;
string xStr = x.ToString("+0.#####;-0.#####");
But this wouldn't help to display trailing decimal point. You can handle such situations using this method:
public static string MyToString(double x)
{
return x == Math.Floor(x)
? x.ToString("+0;-0;0") + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
: x.ToString("+0.####;-0.####");
}
You can try like this:
string myFormatedString = number.ToString("+#;-#");
The format string you want to use is
ToString("N", CultureInfo.InvariantCulture) // Displays -12,445.68
See here for additional options for format strings

Format a number to always have a sign and decimal separator [duplicate]

This question already has answers here:
Custom numeric format string to always display the sign
(6 answers)
Closed 8 years ago.
I want to format any number (integer or real) to a string representation which always has a sign (positive or negative) and a decimal separator, but no trailing zeroes.
Some samples:
3.14 => +3.14
12.00 => +12.
-78.4 => -78.4
-3.00 => -3.
Is it possible with one of the default ToString() implementations, or do I need write this myself?
Try something like this:
double x = -12.43;
string xStr = x.ToString("+0.#####;-0.#####");
But this wouldn't help to display trailing decimal point. You can handle such situations using this method:
public static string MyToString(double x)
{
return x == Math.Floor(x)
? x.ToString("+0;-0;0") + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator
: x.ToString("+0.####;-0.####");
}
You can try like this:
string myFormatedString = number.ToString("+#;-#");
The format string you want to use is
ToString("N", CultureInfo.InvariantCulture) // Displays -12,445.68
See here for additional options for format strings

How to convert a number to a string but formatted with x number of digits [duplicate]

This question already has answers here:
Format string to a 3 digit number
(9 answers)
Closed 8 years ago.
I want to convert a number to a string but have the number formatted with 10 digits. For example, if the number is 5, the string should be "0000000005". I checked the formatting of strings at:
http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx
but there isn't any format that lets you specify the number of digits.
Actually the "0" placeholder would work but in reality I need 100 places, so I'm not going to use the "0" placeholder.
You can use the ToString formatting Dn to output leading zeroes:
var d = 5;
var s2 = d.ToString("D2");
var s10 = d.ToString("D10");
Console.WriteLine(s2);
Console.WriteLine(s10);
The output is:
05
0000000005
Normally the D specifier for standard numeric format strings is enough with its precision to format a number with the required number of leading zeros.
But it stops at 99 and if you really need 100 leading zeros you need to resort to the old trusty method of string concatenation and right truncation
int number = 5;
string leadingZero = new string ('0', 100) + number.ToString();
string result = leadingZero.Substring(leadingZero.Length - 100);
This page should help you find the solution you need: http://msdn.microsoft.com/en-us/library/dd260048(v=vs.110).aspx

Decimal to String Formatting [duplicate]

This question already has answers here:
Truncate Two decimal places without rounding
(24 answers)
Closed 9 years ago.
I try to convert (Decimal)0.9975 to string with (0.##) format in C# but it rounds the number to 1 instead of 0.99
Here is the code;
decimalValue.ToString("0.##");
How can I write the output as 0.99?
I got this on SO long time back. I too was struck with something similar. I owe this post to him.
decimal d = 0.9975m;
decimal newDecimal = Math.Truncate((d*100))/100;
string result = string.Format("{0:N2}", newDecimal.ToString()); // OR
string result = newDecimal.ToString(); //This is simpler I guess.
Hope it helps.
the other option is to accept the rounding but subtract 0.005 from the decimal
decimal d = 0.9975m;
string result = (d-0.005m).ToString("0.##");
(0.9975 - 0.005) = 0.9925;
0.9925 => 0.99
use format
decimalValue.ToString("#0.0#");
The '#' will be updated if there is a value on the placeholder, if there is no value on the'#' placeholder, then this will be ignored, but the '0.0' will not be ignored.
or
var value = string.Format("{0:0.00}", decimalValue);
or
decimal decimalValue = 0.9975;
value.ToString("G3");
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

int to hex string

I need to convert an int to hex string.
When converting 1400 => 578 using ToString("X") or ToString("X2") but I need it like 0578.
Can anyone provide me the IFormatter to ensure that the string is 4 chars long?
Use ToString("X4").
The 4 means that the string will be 4 digits long.
Reference: The Hexadecimal ("X") Format Specifier on MSDN.
Try the following:
ToString("X4")
See The X format specifier on MSDN.
Try C# string interpolation introduced in C# 6:
var id = 100;
var hexid = $"0x{id:X}";
hexid value:
"0x64"
Previous answer is not good for negative numbers. Use a short type instead of int
short iValue = -1400;
string sResult = iValue.ToString("X2");
Console.WriteLine("Value={0} Result={1}", iValue, sResult);
Now result is FA88
Convert int to hex string
int num = 1366;
string hexNum = num.ToString("X");

Categories

Resources