Currently I am doing this way.
string strPoint = "12.5";
string strComma = "12,5";
Console.WriteLine("strPoint: " + float.Parse(strPoint,System.Globalization.CultureInfo.InvariantCulture));
Console.WriteLine("strComma: " + float.Parse(strComma,System.Globalization.CultureInfo.InvariantCulture));
Result:
strPoint: 12,5 and strComma: 125.
strComma must be 12.5? what could be the reason behind this. Please advise.
Remove the InvariantCulture from second Parse.Use your current culture.The decimal separator of InvariantCulture is dot,not comma.You can verify that using :
CultureInfo.InvariantCulture.NumberFormat.CurrencyDecimalSeparator;
In first code snippet you are using dot as a separator and using the InvariantCulture for Parse and it is parsing correctly because the InvariantCulture uses dot as a separator.
In the second code snippet you are using comma and it is truncated because it is not the decimal separator of InvariantCulture, the same culture can not use two different separators at the same time.
In InvariantCulture, the comma is the thousands separator, and for correct strings, the result of parsing cannot depend on whether the thousands separator is present (1000 and 1,000 are two different representations of the same number). float.Parse, however, does not enforce that the thousands separator is only used in the appropriate places, it simply skips it entirely.
I think "fLoat.parse()" will treat "," as a group separator not as decimal separator which is "."
Hence Group separator in this case will always be vanish in output mentioned.
Related
Hi I just want to ask about how can i add padding on string.Format so that when I show it , the mask is applied with leading zeros
Heres my c# code
Model.Phone = String.Format("{0:(###) ###-####}", double.Parse(#e.Phone));
Expected result should be
(012) 345-6789
But the results I am getting is
(12) 345-6789
and the leading zero is missing, Hope someone can help me in this problem , Thanks
You would use 000 instead of ###, read more about format in MSDN article Custom Numeric Format Strings
String.Format("{0:(000) ###-####}", double.Parse(#e.Phone));
Format specifier "0"
Replaces the zero with the corresponding digit if one is present;
otherwise, zero appears in the result string.
Format specifier "#"
Replaces the "#" symbol with the corresponding digit if one is
present; otherwise, no digit appears in the result string.
In some legacy .NET code I've come across a number of custom numeric format strings like this:
###,##0.00
What is the difference between this and:
#,#0.00
?
EDIT:
Here are some example inputs I've tried, all of which yield the same result for both masks: 1000000, 1000, 100, 10, 1.456, -30000, 0.002.
EDIT:
#Sahuagin suggested that these masks could be the same because of how the culture is set to group to three digits. However, even using this I can't demonstrate a difference:
var culture = new CultureInfo("en-US");
culture.NumberFormat.NumberGroupSizes = new[] { 1, 2, 3, 4 };
culture.NumberFormat.NumberGroupSizes.Dump();
1234567890.ToString("#,#0.00", culture).Dump(); // 1234,567,89,0.00
1234567890.ToString("###,#0.00", culture).Dump(); // 1234,567,89,0.00
More generally, I understand that # is an "optional" digit which won't create leading or trailing zeroes. However, it seems like just a single # before the decimal point is enough to get all leading digits. The MSDN docs seem to differentiate between # and ## but the explanation doesn't make much sense to me and I haven't found an example where it makes a difference.
# indicates a place where a digit will appear, if one exists in the number. Absent any such symbols, leading digits automatically appear (though you must have at least either one # or one 0, or no number will appear), but they are still useful as placeholders in some kinds of formats, for example a telephone number:
var value = 1234567890;
Console.WriteLine("{0:###-###-####}", value);
// outputs 123-456-7890
In your example of #,#0.00, I think that only manages to still format correctly (with groups of three) because , is a special grouping symbol, and the culture info is set to group digits in threes. Without that, you would get something like 123-45.67, if you used a - instead of a , for example.
Here is more specific information about , from MSDN:
The "," character serves as both a group separator and a number scaling specifier.
Group separator: If one or more commas are specified between two digit placeholders (0 or #) that format the integral digits of a number, a group separator character is inserted between each number group in the integral part of the output.
The NumberGroupSeparator and NumberGroupSizes properties of the current NumberFormatInfo object determine the character used as the number group separator and the size of each number group. For example, if the string "#,#" and the invariant culture are used to format the number 1000, the output is "1,000".
Number scaling specifier: If one or more commas are specified immediately to the left of the explicit or implicit decimal point, the number to be formatted is divided by 1000 for each comma. For example, if the string "0,," is used to format the number 100 million, the output is "100".
So in your first example of ###,##0.00, it could probably be reduced to #,0.00, if desired, although #,##0.00 is what I usually use since it is much more clear.
I need for this to work in a single format statement and to work for both ints and decimals:
For example:
int myInt=0;
decimal myDecimal=0.0m;
// ... Some other code
string formattedResult1=String.Format("{0}",myInt);
string formattedResult2=String.Format("{0}",myDecimal);
The expected results are:
"" (i.e., string.Empty) if the item to be formatted is zero
and a numeric value (e.g., "123.456" for the decimal version) if it isn't.
I need for this to occur exclusively as a result of the format specification in the format string.
This should do:
string formattedResult1 = string.Format("{0:0.######;-0.######;\"\"}", myInt);
The colon introduces a numeric format string. The numeric format string is divided into 3 parts with semicolons: part 1 is for positive numbers, part 2 for negative numbers, and part 3 for zeros. To define a blank string you need to delimit it with double quotes otherwise it doesn't like it.
See MSDN for the full syntax.
based from the accepted answer above i have done the same thing in microsoft "report builder"
this worked for me (shows 2 decimal places, blank for zero) :
,##0.00;-#,##0.00;""
I would like to represent the number 2.3421 as 2.34 but my current formatting shows it as 02.34
If I had the number 1342.323 I would want to show this as 1,342.32
If I had 0.23 this would be shown as 0.23.
What do I change my format string to achieve this? I have:
"{0:0,0.00}"
Use # where a number is optional instead of 0:
"{0:#,0.00}"
See Custom Numeric Format Strings on MSDN:
"#" | Digit placeholder
Replaces the pound sign with the corresponding digit if one is present; otherwise, no digit appears in the result string.
Try this:
{0:#,##0.00}
1342.323 should then be 1,342.32
I'm importing a csv file in C#, sometimes with '.', sometimes with ',' as decimal separator.
Is there a best way of determinate the decimal separator better than counting from the last char down to the first apperance?
Thanks in advance.
Franklin Albricias.
If you know the correct culture in advance (for example, because you know the user that created the file), you can try to parse the provided value using the appropriate CultureInfo or NumberFormatInfo:
Decimal value = Decimal.Parse(input, new CultureInfo("es-ES"));
But if the type is not known in advance, you'll have to check it manually by examining the characters until you find a separator. (And even that approach assumes that you are guaranteed to always have a decimal separator, such that one is written as 1.0 rather than 1.)
You can't just try each expected format one after the other because you may get false positives.
10,000 means something valid but different for both formats.
Why not use both as a separator?
Have a look at NumberFormatInfo
Edit:
For each value try to parse it with one of the separators.
If that fails try to parse it with the other.
This depends on the actual data stored in the csv file and the data separation character (';' or ',' or ' ').
If all data is always in floting point notation you can use a regular expression that checks both cases. You can use "d+,\d+" to check for values separated by ',' or "\d+\.\d+" for values using '.' as separator
Under the assumption that the file contains only numbers - no strings and what ever - and there are at least two columns, you can do the following.
Go through the first line and look for a semicolon. If you find one, you have semicolon separated numbers with commas as decimal separator, else comma separated numbers with points as decimal separator.
In all other cases you will have to use a heuristic (and sometimes get the wrong conclusion) or you have to strictly parse the file under both assumptions.