String to Numeric conversion and group separator - c#

When I try to convert a string to a numeric value with Parse or TryParse or Convert.ChangeType, I can't manage de thousand separator as it is defined in the system:
if I enter :
var d = double.Parse("1,234", CultureInfo.CurrentUICulture);
it does not return 1234.
If I want to use the group separator, I must type :
var d = double.Parse("1,234", NumberStyles.Number, CultureInfo.CurrentUICulture);
This time, the result is that one expected.
But, I don't want to force the use of the thousand separator, I want use it only if the system specify it in the globalization settings. Is there a way to know if the separator is used (I know that I can read the group separator in CultureInfo.CurrentUICulture.NumberFormat.NumberGroupSeparator)
Cheers
Loic

Having Number (which includes AllowThousands) doesn't demand a comma - it simply allows it. So you could use Number with or without the comma. Or use Any or AllowThousands.
Note that "comma" is swappable with "thousands separator" - i.e. in some of Eurupe it may vary (period etc). If you mean "comma is thousands" then use a fixed culture (such as InvariantCulture).

You could always use the second line you use
var d = double.Parse("1,234", NumberStyles.Number, CultureInfo.CurrentUICulture);
as it will correctly convert 1234 as well as 1,234 and it should cover 1.234 on systems that use '.' as the group separator

You should parse user input using CurrentCulture and not CurrentUICulture.
The property CurrentUICulture refers to the language on which the user interface is displayed and CurrentCulture to the current locale specified in Windows. This way a user working with an application that provides a user interface translated to english (CurrentUICulture) can still view/enter, for example, dates and numbers formatted as the locale that he set in Windows (CurrentCulture).

Related

C# Currency Formatting

Is there any way to format a number in the currency format (in the current Culture) using a custom format string?
For example:
1525.00 -> $1,525 (no trailing zeros)
1525.25 -> $1,525.25 (show decimals only when necessary)
We have tried different formats but none of them can produce the above.
As far as I know, no, you can't do that with a "one" format since you don't want show decimal parts for the first one but you "want" to show decimals part for the second one. I don't think there will be a "simple" format for both.
You can check the Currency format specifier (C) for that as;
var v = 1525.00;
$"{v:C0}".Dump();
returns $1,525 and
var v = 1525.25;
$"{v:C2}".Dump();
return $1,525.25.
Just a note, string interpolation uses CurrentCulture settings and in that case, I assume your current culture is somewhat based on english-based or InvariantCulture for generate CurrencySymbol, NumberGroupSeparator and NumberDecimalSeparator as $, , and . respectively.
Does this count?
static string MyCustomCurrencyString(decimal d) => d % 1 == 0 ? $"{d:C0}" : $"{d:C2}";
Let's test it:
Console.WriteLine(ZerosAreBad(1525));
Console.WriteLine(ZerosAreBad(1525.25m));
It works!
$1,525
$1,525.25

C# Check if string is in a value format

I'm wondering if there's simple way to check if a string is in a value format of any kind. Meaning the number one-thousand could be represented in any of the following ways:
1000
1000.00
1,000
1,000.00
$1,000.00
I've tried double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out n) but that isn't working for items with the currency symbol out in front. So I tried double.TryParse(value NumberStyles.Any | NumberStyles.AllowCurrencySymbol, CultureInfo.InvariantCulture, out n) but that is also not working.
Basically I'd like to check if a value can be considered a number value at all.
The invariant culture does not use $ as its currency symbol; it uses ¤.
Picking the correct culture for parsing is tricky. Obviously, it is best to use the same culture that was used to format the value to begin with. If you know which culture was used, you’re lucky :).
By default, unless a specific culture is specified, CurrentCulture will be used. The current culture can be overridden and set on a per-thread basis, but it defaults to the locale settings of the host operating system. In the absence of other information, CurrentCulture is a reasonable first guess.
I would suggest you || together at least two TryParse calls: one for CurrentCulture (or your best guess as to the culture of origin), and a fallback using InvariantCulture. Include AllowCurrencySymbol on both.
You can use regular expressions
https://regexone.com/references/csharp
you can use below code to accept your cases
1000
1000.00
1,000
1,000.00
$1,000.00
string pattern = #"^\$?(\d{1,3}(\,\d{3})*(\.0{2})?)|(\d+(\.0{2})?)$";
Match result = Regex.Match("$100,000.00", pattern);
if (result.Success)
// your input is in format

String.Format an integer to use a thousands separator with decimal value in danish culture

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")%>

Converting string to decimal: how to handle the decimal separator in different cultures

I need to write decimal value to ms access database, but i have a problem with conversion values to decimal in different cultures. Have a values from file, which separates by commma. I try:
public decimal CSingleCulture (string str)
{
string sep = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
string s = str.Replace(",", sep);
return decimal.Parse(s);
}
if NumberDecimalSeparator = "." then work is good, but if NumberDecimalSeparator = "," problems begin... decimal.Parse(s) always return vlaues separates by dot. In this situation, when inserted into a database error occurs.
The recommended way to deal with this is to store the value as a number rather than a string. Both in the database and in your program. When you do that, your current problem simply never arises.
The only time you deal with numbers in string format is when you display them, or accept user input. In those scenarios you can use the user's culture settings to let them see and use their preferred separator.
Should you ever need to convert between string and number for persistence then you must use culture invariant conversion. This appears to be where you are falling down. I suspect that the file you read has no well-defined format. Make sure that when you read and write the file you use CultureInfo.InvariantCulture. If the file does have a well-defined format that differs from the invariant culture, then use an appropriate specific CultureInfo.
Can't actually understand what is it you're trying to accomplish, and I have to agree with the other answer. But one other thing that's good to know is you can use invariant culture like so:
double.Parse("15.0", CultureInfo.InvariantCulture)
This will always expect dot character to delimit your decimal digits regardless of what is set in current thread's culture.

How to format string with floating point?

In database I have a PRICE field type of float with value 54342.76 and I want to display it on gridview as 54,342.76. How can format this values?
Try
float f = 54342.76F;
string s = f.ToString("0,0.000", CultureInfo.InvariantCulture);
Console.WriteLine(s);
You could use c specifier instead, however it prints currency sign also.
Use CultureInfo.InvariantCulture as in some localizations , thousands separator may be missing.
Also read Decimal.ToString Method, Standard Numeric Format Strings, Custom Numeric Format Strings
this is what I use:
x.ToString("c")
String.Format("{0:n}", 54342.76F)
The N method is a good solution since it should respect the user's locale while others like:
String.Format("{0:#,###,###.##}", 54342.76F)
Could bypass current culture in some situations. Use {0:n0} instead of {0:n} if you want to display the number without decimals.
In the past I have used this: http://www.codeproject.com/Articles/11531/Money-DataType
It formats money perfectly when used in a DataGridView column.

Categories