I am getting some percent values from a database and I need to format them to have the correct thousands seperator, number of decimal places and a percent sign on the end.
I tried this:
string text = "105,3"; //example, formatting like database input
string format = "#,##0.##";
e.Row.Cells[i].Text = double.Parse(text).ToString(format);
Weirdly this returns 1053,00%. How do I make it so it returns 105,30%? (The decimal comma is because the system locale is german, so it's how it is supposed to be)
edit: replacing the comma with a period results in 10530.00%. Nothing makes sense to me anymore.
edit2: the float.Parse() actually works just fine. the ToString() messes everything up. I played around with using different cultural settings and format strings (switching comma and period) but it only makes it worse again.
Pass the current Culture to the Parse method: double.Parse( text, CultureInfo.CurrentCulture )
However, this only works on systems that use a locale that has the comma as a decimal separator.
If you want this to work on other locales you should replace CurrentCulture with the specific CultureInfo instance that used when inputting data in the first place.
The title is misleading. The actual problem was the ToString() function. In the format string I added the % sign, which, to be fair, I didn't add in the original post because I forgot about it. It automatically multiplies the number by 100. So my format string is now "#,##0.00\%".
Related
I have this tag in a XML
<seguroTotalReais>000000000037103</seguroTotalReais>
I need to set this value like a decimal with 2 places for example 371,03.
Is there a way to do this without manipuling string directly?
You have a couple of options, personally I would not do string operations on this. If you are looking to shift the decimal 2 places i would convert and then divide by 100. This will also give you some validation on whether its a numeric value or not.
string dec = "000000000037103";
// I did a string replace for swapping the dot to a comma.
Console.WriteLine((Convert.ToDouble(dec)/100).ToString("0.##").Replace(".",","));
You can also utilize Culture info to affect how your numbers are displayed as some culture info's utilize the comma as the decimal separator.
I want to pass decimal value to database table. When I send value 1,5 to database then save value as 15.
Code:
txtKm.Text="1,5";
zelezniceDataSet.VozniRediRow row;
row.Km = decimal.Parse(txtKm.Text);
zelezniceDataSet.VozniRedi.AddVozniRediRow(row);
vozniRediTableAdapter.Update(zelezniceDataSet.VozniRedi);
When debug decimal parsing the value is correct (1.5)
You probably want to look at an overloaded form of decimal.Parse that accepts a culture-specific format option. My guess is decimal.Parse sees your comma as a thousands-separator and just removes it.
seems to me that decimal.Parse is ignoring the comma ... if you pass "1.5" is the correct value saved? If so then you need to use the overload that takes the culture to use when parsing Decimal.Parse(String, NumberStyles) and pass it the correct number style for the language you are using.
EDIT ... more info ..
decimal.Parse(test, CultureInfo.GetCultureInfo("EN-us").NumberFormat );
will output 15 for test = "1,5" because in English the "," char is the thousands separator
decimal.Parse(test, CultureInfo.GetCultureInfo("NL-nl").NumberFormat );
will output 1.5 for test = "1,5" because in Dutch the "," char is the decimal separator
Hope this helps :)
I have a requirement to display all currencies in the following format:
"[Code] [Symbol]#,##0.00;[Code] [Symbol](#,##0.00)"
The user will chose a country and I get the currency code and symbol from an external service. I then use string.replace to set the currency code and symbol into the format string above.
So the value 123.456 (US Dollars) would be formatted as follows:
"USD $123.46"
This approach works for most currency symbols but I am having a problem with the currency symbol for Kuwait د.ك. I cannot seem to format any value to allow the symbol to appear before the numbers. I don't have the same problem with letters. I am thinking that this has to do with the fact that Arabic is written from right to left. The symbol even behaves that way in browser. I can't select it from left to right, selection starts from the right.
Also when I was testing in linqpad, my string was displayed as [Kuwait_Symbol]#,##0.00, however pasting in the stackoverflow textbox caused the string to be د.ك#,##0.00
Note that the ordering of the characters in the string has changed.
Though, I have a feeling that I will have to recommend that using symbols are not feasible, is there any way to get around this problem? Also, why can't the symbol be displayed before numbers?
The issue stems from browsers and how they handle characters in right-to-left languages rather than the code that generates them. If you try
var kuwaitCulture = new System.Globalization.CultureInfo("ar-KW");
Response.Write(Server.UrlEncode(amount.ToString("c", kuwaitCulture)));
you'll see that what gets returned clearly has the CurrencySymbol before the amount.
To get around this you can force left-to-right rendering in the browser with the corresponding Unicode character. For example,
Response.Write('\u202A' + kuwaitCulture.NumberFormat.CurrencySymbol + '\u202C' + amount);
renders correctly as per your specification.
I have a function that parses a string containing a date(and/or time) e.g. "2009-12-10". I get the order of year-month-day from the Short Date pattern. When going through the string I use Char.IsSeparator(ch) to figure out when the numbers end.
Now however in the case of Korean it seems the Char.IsSeparator(ch) returns false on separator characters. Is there any way to know whether the chars in between the numbers are separator regardless of region setting?
(I also parse strings that are more free containing things like "*20 May 200*9" so doing Char.IsAlphaNum() on the separator will not work either as I don't know the content basically)
Example inputs: "20.10.2009" "2009-05-20" "20 May 2009" "20.05.2009 10:00 AM" "1/1/2009" (in Singapore its D/M/Y in US it is M/D/Y") "Tisdag, 1 Januari 1962" (all strings localized)
Output would be an equivalent of a DateTime instance filled as much as possible (although we use our own types).
Korean seems to have a couple of characters in front of the time and as separator it looks like the symbols are different depending on position in the string.
If you pick up the format using the current short format, you could perhaps also be able to pick up the separator through DateTimeFormatInfo.CurrentInfo.DateSeparator.
Is there any reason why you need to parse the string manually?
If you used the built-in date/time parsing methods - Parse, ParseExact, TryParse or TryParseExact - then you could pass in the required culture-specific format info and let the framework worry about separators etc.
I am trying to format a double in C# such that it uses the thousand separator, and adds digits upto 4 decimal places.
This is straight forward except that I dont want to have the decimal point if it is an integer. Is there a way to do this using the custom numeric format strings rather than an if statement of tenary operator?
Currently I have:
string output = dbl.ToString(dbl == (int)dbl ? "#,##0" : "#,##0.####");
Thanks
I believe your second format string of "#,##0.##" should be exactly what you want -- the # format character is a placeholder that will NOT display zeros.
If you had "#,###.00" then you would get trailing zeros.
test code:
double d = 45.00;
Console.Writeline(d.ToString("#,##0.##"));
Gives output of "45". Setting d to 45.45 gives output "45.45", which sounds like what you're after.
So you had the answer after all! ;)
Incidentally, there's a handy cheat-sheet for format strings (amongst other handy cheat-sheets) at http://john-sheehan.com/blog/net-cheat-sheets/
No, there is not any built-in format string for this. Your current solution is the best way to accomplish this.
MSDN lists both the standard numeric format strings and custom numeric format strings, so you should be able to see for yourself that none directly matches your needs.