Where can I set style in which double is writte by ToString() method ?
I am getting for example 2,2345 while I want to have 2.2345
thanks for ay hints,
bye
double a = 2.2345;
string b = a.ToString(CultureInfo.InvariantCulture);
You need to specify a FormatProvider, ususally as a CultureInfo.
For example:
string s = d1.ToString(System.Globalization.CultureInfo.InvariantCulture);
The Double.ToString() method is overloaded, so you can call it with a couple of different signatures to get the output you want.
In this case, your culture settings are affecting the output of the ToString() function. To get the result that you want, you should call Double.ToString(IFormatProvider), passing in CultureInfo.InvariantCulture:
myDouble.ToString(CultureInfo.InvariantCulture);
This should solve your issue.
For future reference, note that another common version of Double.ToString() is the Double.ToString(String) overload. The String parameter is a numeric format string, either one that is predefined or one that you specify. For example:
myDouble.ToString("format string here");
MSDN has a couple of articles on format strings: Standard Numeric Format Strings and Custom Numeric Format Strings.
If you would want the affect to be on your whole application. This could be set on initialization. Thus, note that this would mean an effective change on ALL cultures.
var culture = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name);
culture.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = culture;
Then just:
string value = (1002.300).ToString();
Related
Why are these 2 lines show different values? Is it because {0:18}"? Why? Thanks.
Console.WriteLine(value.ToString("C", CultureInfo.GetCultureInfo("da-DK"))); //-12,46 kr.
Console.WriteLine("{0,-18}",value.ToString("C"), CultureInfo.GetCultureInfo("da-DK")); //($12.46)
Is it because {0:18}?
No. In the first case, you're formatting your value as string a using the "C" format specifier, and using the Danish culture.
In the second case, you're formatting your value as string a using the "C" format specifier using the default culture... and then including that string value in another format operation. You're passing the Danish culture as a second, unused format argument, but even if you passed it in the right place, it would be irrelevant by that point, as when you format a string, it always just stays as it is, regardless of culture.
I suspect you actually want this:
string text = value.ToString("C", CultureInfo.GetCultureInfo("da-DK"));
Console.WriteLine("{0,-18}", text);
Or to do all the string formatting in one operation:
string text = string.Format(CultureInfo.GetCultureInfo("da-DK"), "{0,-18:C}", value);
Console.WriteLine(text);
(As far as I can tell, Console.WriteLine has no overload permitting the culture to be specified.)
The culture parameter treats differently in these calls; for the 1st fragment
Console.WriteLine(value.ToString("C", CultureInfo.GetCultureInfo("da-DK")));
we have value converted to string treating value as being currency ("C") of Danemark (CultureInfo.GetCultureInfo("da-DK")). On the contrary for
Console.WriteLine("{0,-18}",value.ToString("C"), CultureInfo.GetCultureInfo("da-DK"));
we have efficiently String.Format ("{0,-18}") call with two parameters:
value.ToString("C") - value as default culture (not necessary da-DK) currency, so it can be, say, "15.47$"
CultureInfo.GetCultureInfo("da-DK") which is ignored (no {1} in the format)
I have a string totalPRice which holds a value like this 1147,5
I want two things.
1)round the value so that there is always two digits after ,
2)Implement thousands separator in this string, So that final out put will be some thing like this 1.147,50
I have tried some thing like this
String.Format("{0:0.00}", totalPRice)
It does my first requirement correctly by producing an output 1147,50.
But I am way behind in my second requirement. Can any one tell me how I can achieve this?
Note: In danish culture . stands for , and , stands for .
You can refer to Standard Numeric Format Strings and use
string.Format("{0:N2}", 1234.56)
You may also specify the culture manually, if danish is not your default culture:
var danishCulture = CultureInfo.CreateSpecificCulture("da-DK");
string.Format(danishCulture, "{0:N2}", 1234.56);
see MSDN Reference for CultureInfo
You should create a culture-specific CultureInfo object and use it when converting the number into a string. Also, you can set the default culture for your whole program.
Then, your code will look like this:
// create Dennmark-specific culture settings
CultureInfo danishCulture = new CultureInfo("da");
// format the number so that correct Danish decimal and group separators are used
decimal totalPrice = 1234.5m;
Console.WriteLine(totalPrice.ToString("#,###.##", danishCulture));
Note that . and , in the formatting string are specified opposit as you want. This is because they identify decimal and group separators, and are replaced with the correct culture specific-ones.
Try this:
String.Format("{0:N2}", totalPRice)
Another possibility is to use the ToString(string format) overload.
totalPRice.ToString("N2");
If this is a currency value (money!), then it's better to use the current format specifier 'C' or 'c':
string.Format("{0:C}", 1234.56)
Normally I don't write the number of decimal digits since it comes from the international configuration.
You may way to use a different colture specifier if you don't want to use the default one.
var colture = CultureInfo.CreateSpecificCulture("§§§§§");
string.Format(culture, "{0:C}", 1234.56);
where §§§§§ is the string that identifies the desired colture.
Try this for Price format. Put it under template field instead of BoundField.
<%#(decimal.Parse(Eval("YourDataField").ToString())).ToString("N2")%>
I have textbox that accept numbers. Those numbers will be saved in database.
When I enter number like 2,35 and convert to float and send to database I get error because database accept only number with dot, e.g. 2.35
float num = float.Parse(textBox1.Text);
num is still 2,25
How to manage that? I've tried with CultureInfo.InvariantCulture but I never get what I want
You can try the following:
float.Parse(textBox1.Text.Trim(), CultureInfo.InvariantCulture.NumberFormat);
I hope this would've solved the issue.
The easiest way is to replace ',' with '.' in like:
float num = float.Parse(textBox1.Text);
string stringValue = num.ToString().Replace(',', '.');
Then send "stringValue" to database.
I hope that helps you.
use this:
CultureInfo.CreateSpecificCulture("en-US");
I have same problem back then and it solved by code above
num is 2,25 because it's shown to you in your culture. It will be passed correctly to the database, provided you use the usual mechanisms (i.e. prepared statements with parameters). If you insist on manually gluing together SQL, then by all means use InvariantCulture to format the number, but generally, please don't.
This is a common globalization issue. What you have to define is a single culture in which to store the data itself, since you are storing it as a string value. Then, do ALL your data input and handling using that culture. In our code, we have several blocks that look similar to this in order to handle multi-cultural math and data display:
//save current culture and set to english
CultureInfo current = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
//Do Math and Data things
//restore original culture
System.Threading.Thread.CurrentThread.CurrentCulture = current;
This way you can make sure that all the data is handled and stored the same way, regardless of the culture being use to generate or display the data.
Edit
To do the data save, and converting the number to a string, you would do things exactly the same way. While you have the current threads CultureInfo setting as "en-US", the .ToString() methods from all the numbers will use "." instead of "," for the decimal point. The other way to do it is specify a format provider when calling .ToString().
decimalNumber.ToString(new CultureInfo("en-US"));
This specifies that when you convert the number to a string, use the NumberFormat from the provided culture.
In Form_load I have
txtAlteFonduri.Text = "5,00";
txtFReparatii.Text = "15,00";
txtFRulment.Text = "20,00";
and in another function I want to parse text as decimal
decimal alteFonduri = Decimal.Parse(txtAlteFonduri.Text);
decimal fondRulment = Decimal.Parse(txtFRulment.Text);
decimal fondRepar = Decimal.Parse(txtFReparatii.Text);
but I have an error in the second line
Input string was not in a correct format.
You need to specifically add the number format. For your examples above, the following should work:
decimal alteFonduri = Decimal.Parse(txtAlteFonduri.Text, CultureInfo.GetCulture("de-DE"));
Otherwise, the system's culture information is used.
You are using a different culture to what decimal.Parse() is expecting (it expects the decimal point '.' but you provide a comma. Using the correct culture should correctly parse the strings, although I can run your code without having any errors...
You can use Decimal.Parse(variable, CultureInfo.GetCultureInfo("Culture-Name"));
You have to use this overload of Decimal.Parse and supply a IFormatProvider matching the culture of your input. You should also consider using one of the Decimal.TryParse methods for better error handling.
in my application i am update my ui with my label and i want to show the number in #,##0 format.
myClass.getNumberOfFiles return string.
myLabel.Text = myClass.getNumberOfFiles();
Assuming getNumberOfFiles returns a string (which, by its name, it shouldn't) :
myLabel.Text = int.Parse(myClass.getNumberOfFiles()).ToString("#,##0");
I suspect you want the standard "numeric" format specifier, with a precision of 0:
label.Text = GetNumberOfFiles().ToString("N0");
This is after you've fixed your getNumberOfFiles() method to be GetNumberOfFiles() (naming convention) and made it return int or long (a method which is meant to fetch a number should not return a string).
This will use the appropriate grouping for the current culture; if you want a different culture you can specify it as a second argument.
int files;
if (int.TryParse(myClass.getNumberOfFiles(), out files)) {
myLabel.Text = files.ToString("N0");
}
This won't work if you have any formatting in the number already I think. It will work though if on the return of getNumberOfFiles() someone was turning an int into a string. If getNumberOfFiles() is returning a formatted string, you will need to do some different stuff. Below assumes the formatting is in the en-US format coming in and you want to display it in Brazilian Portuguese for example. It is shown in a verbose manner so you know how to plug other cultures in if you need to. If its formatted and doesn't need to change between cultures I don't know why you couldn't just assign the return of getNumberOfFiles() directly to the label's Text property.
int files;
var incomingCulture = CultureInfo.CreateSpecificCulture("en-US");
var outgoingCulture = CultureInfo.CreateSpecificCulture("pt-BR");
if (int.TryParse(myClass.getNumberOfFiles(), NumberStyles.Number, incomingCulture, out files)) {
myLabel.Text = files.ToString("N0", outgoingCulture);
}
That being said I agree with all the others saying it is ridiculous to return a string for an integer. But I know sometimes you don't have the luxury of being able to change it.
I'll also point out that if you use the named format specifiers like "N0", one day a programmer coming behind you will bless you in their heart when they have to globalize your code. This is because every CultureInfo instance has an implementation for each of the named formats, however it is impossible for it to have implementations for custom format specifiers.