double s = 12.20;
Console.WriteLine(s);
It prints 12.2 but I need it to print 12.20. How can I do this?
You can specify the format for converting a number to a string - here's one way of many. Google ".net numeric format strings" for more...
Console.WriteLine(s.ToString("0.00"));
quoting this
Console.WriteLine(value.ToString("F")); // Displays -16325.00
try s.ToString("0.00");
Related
For example is it possible to do
WriteLine("Statement : {0:c}", 23455);
WriteLine("Statement : {0:n}", 23455);
In one line of code using WriteLine?
To get the output in the format $23,455 you would use {0:c0}, the final 0 tells how many decimal places to show, it defaults to {0:c2} if you don't specify a number.
I have a string with value "20.616378139" and when i try to convert using Convert.ToDouble or Double.Parse i get 20616378139.0 insted of the right value.
Why is this happening and how should I fix it?
You probably live in a part of the world where the decimal point is written as a comma. Fix:
var str = "20.616378139";
var dbl = double.Parse(str, System.Globalization.CultureInfo.InvariantCulture);
There's an overload to the Parse method that provides an options parameter of some kind; this is the way that you can specify for it to handle scientific notation, etc. Try setting that explicitly. If that works, then look at the default culture info settings you are using.
I've used this command and there is no problem for me before.
string s = "20.616378139";
double d = Convert.ToDouble(s);
![enter image description here][1]
this may be simple but im still new to this language.
I'm creating a windows application where is a text box and a a button. What it will do is take the value entered in the text box and create a .txt file with the value entered in the textbox as its content.
I've don this. I have successfully add "5" to the textbox and pressed the button and it will create a txt file with content "5" in it.
My question is, how do I format the value form the text box to something like this xxxxxxx.xxxxx?
So if I enter 5 how do I make it create in text to become 0000005.00000?
Or if I enter 5.4 how do i make it become like this 0000005.40000?
Can anyone shed a light? Or coding sample?
What you have to do is to parse the string from the TextBox into a double and then format it back to string using NumberFormatInfo.
To parse the string, use:
Double d;
Double.TryParse("5.4", out d);
To format to what you want, have a look at these docs and choose the format that you need: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
Parse your TextBox text into a double variable (using double.Parse or double.TryParse) and try something like this:
double d = 5.0d;
Console.WriteLine(string.Format("{0:0000000.00000}",d));
d = 5.4d;
Console.WriteLine(string.Format("{0:0000000.00000}",d));
Try the following:
int number = 5;
string content = number.ToString("0000000.00000");
This blog post is good for how to use string.Format - http://blog.stevex.net/string-formatting-in-csharp/
Also MSDN is very helpful - http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
For example, "if using i enter 5.4 and how do i make it become like this 0000005.40000?":
Take a look at that MSDN link, under heading "The Decimal ("D") Format Specifier":
Console.WriteLine(value.ToString("D8"));
// Displays -00012345
Take a look at that MSDN link, under heading "The Fixed-Point ("F") Format Specifier"
integerNumber = -29541;
Console.WriteLine(integerNumber.ToString("F3",
CultureInfo.InvariantCulture));
// Displays -29541.000
But actually, to achieve what you're looking for then you are probably best using a format string like:
var f = 5.4;
f.ToString("000000000.0000000000000");
// Displays 000000005.4000000000000
First of all, a text box gives you the value as a (text) string. If you want to process it further as a number (which you do), you first need to convert (parse) it. Then, you just format the number the way you want:
var number = Decimal.Parse(textBox.Text);
writer.Write(number.ToString("{0:0000000.00000}", number));
Look up string.Format():
http://alexonasp.net/samples/stringformatting/
How do I get the output of this to read 0 instead of 00 when the value is 0?
String.Format("{0:0,0}", myDouble);
string.Format("{0:#,0}", myDouble);
(tested version)
String.Format("{0:#,0}", myDouble);
Another alternative is to use this:
string s = string.Format("{0:n0}", myDouble);
If you always want commas as the thousands separator and not to use the user's locale then use this instead:
string s = myDouble.ToString("n0", CultureInfo.InvariantCulture);
While the posted answers here ("{0:#,0}") are correct I would strongly suggest using a more readable picture (also to avoid confusion about decimal/thousand separators):
string.Format("{0:#,##0}", v); // to print 1,234
string.Format("{0:#,##0.00}", v); // to print 1,234.56
But all those pictures work the same, including 2 comma's for 1e6 etc.
I noticed my doubles contain different precision characters depending on regional settings.
For example:
3,47 or 3.45
How can I enforce the double should always contain a . precision character?
Problem is once I serialize the class containing this double to XML, it gets shipped to other systems expecting a standard result.
So after reading your responses (and thanks), do you guys recommend changing the property to a string, (making the replacements in a string), so that it serializes with the string value (not the double)?
You need to put the double to string using the Invariant Culture.
double d = 3.47;
Console.WriteLine(d.ToString(CultureInfo.InvariantCulture));
You will need to format with the InvariantCulture.
Note that the "." and "," formatting characters are interpreted according to the culture settings.
This little tutorial will be the answer you need, I expect:
http://www.java2s.com/Tutorial/CSharp/0440__I18N-Internationalization/Parsingnumberswithcultureinvariantparsing.htm
double.Parse(numberString, CultureInfo.InvariantCulture);
The double itself doesn't include a "." or a ",", only the print out of the representation does. You can read up on custom formats here.
[Update according to OP]
I don't know exactly what your design looks like, but it would probably be smart to create a string property on your DTO, which would output the formatted string of your double, and then mark your double property as not serializable.
When you need to do this for all numbers in your current applicaton, you can use the following to set it application-wide (new threads will inherit this setting):
// use this statement to force the default:
Application.CurrentCulture = CultureInfo.InvariantCulture;
string s = myNumber.ToString();
// for one number you have to remember to use:
string s = myNumber.ToString(CultureInfo.InvariantCulture);
Note: by default, your application, whether ASP.NET or WinForms, will use the culture settings of the system it is running on (in ASP.NET, you can set the culture globally in the web.config).