How do I use the ToString method on an integer to display a 2-char
int i = 1; i.ToString() -> "01" instead of "1"
Thanks.
You can use i.ToString("D2") or i.ToString("00")
See Standard Numeric Format Strings and Custom Numeric Format Strings on Microsoft Docs for more details
This should do it:
String.Format("{0:00}",i);
Here's a link to an msdn article on using custom formatting strings:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
In order to ensure at least 2 digits are displayed use the "00" format string.
i.ToString("00");
Here is a handy reference guide for all of the different ways numeric strings can be formatted
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
In C# 6 you could write:
var i = 1;
var stringI = $"{i:D2}";
$ - string interpolation
i.ToString("00") Take a look at this for more rules.
In any case you wanna check first if it's only 1 number, use Regular Expression:
Regex OneNumber = new Regex("^[0-9]$");
OneNumber.Replace(i.ToString(), "0" + i)
Related
Consider this as my input:
number : 123
pattern: %5d
Output in C after string formatting : " 123"
Note: White spaces are displayed in first two positions.
To be exact, my input patterns will be in C language format and I'd like to get the functionality of those formats(result should be same as in C) in C#.
The correct C# syntax for that is:
string.Format("{0,5}", number)
It pads the number by 5 positions (spaces). You can use regex if you want to convert the one to the other.
Or with C# 6 string interpolation syntax:
$"{number,5}"
You could achieve this behavior without using a pattern or Regex. Use the .NET framework string.PadLeft() method instead:
int number = 123;
var result = number.ToString().PadLeft(5);
I think you can use custom numeric format strings. Which can be found here: Custom Numeric Format Strings
I would go with The "#" custom format specifier.
I can't format decimal in custom formatted string
0.656 => 0.67;
23.656 => 23.67;
5105.54 => 5 105.54;
1234567,89 => 1 234 567,89
I found several posts:
c# - Using String Format to show decimal upto 2 places or simple integer
c# - Converting Decimal to string with non-default format
but when try to use them getting several problem
for example:
on value
0.656 i'm getting ".656" or ".66"
23.656 => " 23.656" or " 23.66"
Car someone recommend links where I can find formatstring rules?
I don't think you actually want to convert 0.656 to 0.67, cause it is just wrong. I guess you mean it should display as 0.66
Use
YourNumber.ToString("0.##");
If you really want to have spaces (which again i think it is wrong):
YourNumber.ToString("#,##0.##").Replace("."," ")
Give this a good read:
Custom Numeric Format Strings
You can use String.Format or ToString() overloades to achieve your goal.
If you want to format a number as a currency value, use this
var d = 0.656;
Console.WriteLine("{0:C}", d); // prints "€ 0,66", in my case
Make sure your localization settings give you the correct currency symbol and decimal character, I.E. a point or a comma.
I know I can format strings using the String.Format() method. Is it possible to format like this?
Example:
string: 1568
formatted: 1.568
string: 168794521
formatted: 168.794.521
string: 987
formatted: 987
Sorry that I can't make myself more clear.
You can format a number that way, but not a string. For example, if you have an integer value, you can use:
int value = 168794521;
string formatted = value.ToString("N0");
With the proper culture, this will format as shown.
If you are using a string, you would need to convert it. You could also explicitly provide a culture to guarantee "." as a thousands separator:
int value = Int32.Parse("168794521");
string formatted = value.ToString("N0", new CultureInfo("de-DE"));
string someNumericValue = "168794521";
int number = int.Parse(someNumericValue); // error checking might be appropriate
value.ToString("0,0", CultureInfo.CreateSpecificCulture("el-GR"));
This will put points in for thousand specifiers.
It's possible that if you want this, your culture may already do this.
Yes, you can do that. SteveX has written a great blog post on string formatting. You could also look at this blog post, and the MSDN documentation.
You probably want to look at the bottom of this documentation in the "More Resources" section for more info about different types of standard format strings.
Here is the relavant part from the SteveX blog on formatting numbers:
Currency {0:c}
Decimal (Whole number) {0:d}
Scientific {0:e}
Fixed point {0:f}
General {0:g}
Number with commas for thousands {0:n}
I am trying to format the contents of a text box:
this.lblSearchResults1.Text =
Convert.ToDouble(lblSearchResults1.Text).ToString();
How do I amend this so that I the text includes comma/thousand separators?
i.e. 1,000 instead of 1000.
Looking at the standard numeric format strings:
You can most easily use 'N' which will do the right thing based on the user culture, so in your case you can just add "N" as a param to the ToString
([double]12345.67).ToString("N")
12,345.67
For complete custom control, use ... .ToString("#,##0.00") or variations thereof. The . and , will be replaced by culture dependent symbols. In most of europe you'd get 1.234,56.
Another useful picture is 0.0#.
To use a pattern depending on the users (or on a selected) culture, use The Numeric ("N") Format Specifier, as in .ToString("N") or "... {0:N}".
The easiest way to do it would be something like:
Convert.ToDouble("1234567.12345").ToString("N")
If you want to control the decimal places you can do something like:
Convert.ToDouble("1234567.12345").ToString("N3")
In general look at the overloads on ToString for more exciting possibilities.
If you take a closer look at Standard Numeric Format Strings you can try one of the following:
.ToString("n", CultureInfo.GetCultureInfo("en-US"))
.ToString("n", CultureInfo.GetCultureInfo("de-DE"))
.ToString("n", CultureInfo.CurrentCulture)
An alternative to the above mentioned responses would be to use
this.lblSearchResults1.Text = String.Format("{0:N}", Convert.ToDouble(lblSearchResults1.Text))
If you wanted decimal places, just enter the amount of decimal places you wish to have after the N. The following example will return the value with 2 decimal places.
this.lblSearchResults1.Text = String.Format("{0:N2}", Convert.ToDouble(lblSearchResults1.Text))
See http://msdn.microsoft.com/en-us/library/system.string.format.aspx for more information.
double.Parse(Amount).ToString("N");
Do not cast integral to double to do this!
Use NumberFormatInfo helper class, e.g:
var nfi = new NumberFormatInfo() {
NumberDecimalDigits = 0,
NumberGroupSeparator = "."
};
var i = 1234567890;
var s = i.ToString("N", nfi); // "1.234.567.890"
What will be the equivalent code for Format(iCryptedByte, "000") (VB.NET) in C# ?
String.Format(format, iCryptedByte); // where format like {0:D2}
See MSDN 1, 2, 3
Another very useful site for C# string formatting: http://blog.stevex.net/string-formatting-in-csharp/
Instead of {0:D3} you can also use the zero placeholder, e.g. {0:000} will pad with zeros to minimum length of three.
Microsoft.VisualBasic.Strings.Format(iCryptedByte, "000");
You'll need to add a reference to the Microsoft.VisualBasic assembly.
Given this VB code:
Strings.Format(iCryptedByte, format)
Replace with this C# code:
var csformat = "{0:" + format + "}";
String.Format(csformat, iCryptedByte);
Try:
iCryptedByte.ToString("D3");
see String.Format