C# parsing float from string - c#

I'm reading numbers from XML files. Other numbers are with a comma separator (0,1111) and others with dot (0.1111). How do I parse these numbers so I get the desired result in the end? I tried using float.Parse(reader.Value, System.Globalization.CultureInfo.InvariantCulture); but it doesn't work. For example I have reader.Value = "0,01119703" and is parsed as 1119703.0.

I don't believe that it is possible to work with two different decimal separators at the same time. I think I would just use Replace() to change any commas into dots.
float.Parse(reader.Value.Replace(',', '.'), System.Globalization.CultureInfo.InvariantCulture);

Not sure this is the greatest solution, but perhaps you could rely on a set of known "Custom" number formats. For instance, you could declare two custom number formats (either from scratch or based off of a known format) such as:
private static readonly NumberFormatInfo DecimalSeparatorFormat = new NumberFormatInfo { NumberDecimalSeparator = ".", NumberGroupSeparator = "," };
private static readonly NumberFormatInfo CommaSeparatorFormat = new NumberFormatInfo { NumberDecimalSeparator = ",", NumberGroupSeparator = "." };
And then try parsing the number through your known accepted formats:
if (!Single.TryParse(unparsedValue, NumberStyles.Float, DecimalSeparatorFormat, out parsedValue) && !Single.TryParse(unparsedValue, NumberStyles.Float, CommaSeparatorFormat, out parsedValue))
throw new FormatException("Number format not supported");
This assumes that you have a finite number of known formats, if your inputs can truly be in any culture, then you may be out of luck with finding a great solution.
The one win with this approach is you are at least being explicit in the formats you are able to support rather than relying on a simple string replace (which may result in an invalid format).

Is there anything in the XML files that will tell you which format is being used? There's not a built-in way in .NET to have two different allowed decimal separators. If there's nothing telling you which format a number is going to be in, then you could always check to see whether the string contains a period or a comma, and create a NumberFormatInfo with that as the decimal separator. Of course, this won't work if any of the numbers have a period or comma as a thousands separator.

Related

Float.Parse() ignoring decimal comma

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\%".

Decimal.TryParse - missing separator?

I have strange situation, when I use API. I get object in JSON, which I deserialize. This object contains string property which is parsed to decimal.
To do this I use this code. I live in Poland where decimal separator is ',', so I use replace method.
string input ="160.00"; //value from API
decimal result;
decimal.TryParse(input.Replace('.',','), out result);
From time to time I result is equals 16000!! (I suppose TryParse method delete separator, it not determined).
How can I prevent this situation? Can I parse
Numbers should be serialized as InvariantCulture anyway so the InvariantCulture for parsing is a good start. The code serializing the numbers should also be checked whether it follows this rule.
string input ="160.00";
decimal result = decimal.Parse(
input,
System.Globalization.CultureInfo.InvariantCulture);
Not serializing numbers as culture invariant is one of the most common source of problems like it runs on my machine I have no idea why it doesn't on yours... oh you say your system is in a different language oops ;-)
Instead of replacing decimal point character you should be using a proper overload of TryParse method, i.e. decimal.TryParse(String, NumberStyles, IFormatProvider, Decimal):
string input ="160.00";
NumberStyles style = NumberStyles.Number;
decimal number = 0;
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
decimal.TryParse(input, style, culture, out number)
Make sure to specify the correct culture which is suitable for your case.

Get Default NumberFormat for Currency based on User Language

I developed a Excel add-on so that users can insert some numbers from server to their worksheets. Now I am trying to format cells with inserted data.
Because my users belong to various countries,
the add-on should format based on their culture.
Some use period as decimal separator, some use comma.
Some use comma as thousand separator, some use blank or period.
Some use fraction currency, some not.
Now I use NumberFormatInfo class and generate NumberFormat string manually.
Here is the code:
NumberFormatInfo nfi = NumberFormatInfo.CurrentInfo;
StringBuilder sbFormat = new StringBuilder();
sbFormat.Append("#");
if (nfi.CurrencyGroupSeparator == "?")
sbFormat.Append(" ");
else
sbFormat.Append(nfi.CurrencyGroupSeparator);
sbFormat.Append("##0");
if (nfi.CurrencyDecimalDigits > 0)
{
sbFormat.Append(nfi.CurrencyDecimalSeparator);
sbFormat.Append(new string('0', nfi.CurrencyDecimalDigits));
}
cell.NumberFormat = sbFormat.ToString();
I have no idea this code covers all patterns or not.
Hoping to use something that Excel provides as default, but I could not find that.
Any idea?
Thanks!

Formatting strings into groups of 3 digits by periods

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}

How to insert a thousand separator (comma) with convert to double

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"

Categories

Resources