Hi i have a decimal value witch i'm trying to remove the white space when the value is over a thousand.
When a value is returned with over a thousand the number is returned something like "2 000", the white space is causing problems.
I have tried replace and trim and just can't find a way to remove the white spaces.
decimal fee = AdministrationDataManager.AdminMarkupForPriceSelect(price, isProduct, companyId);
string Fee = (fee.ToString("N2"));
string newFee = Fee.Replace(" ", string.Empty);
newFee = newFee.Trim();
return (newFee);
The formatting string "N2" format numbers with thousand separators. If you don't want thousand separators, use "F2" instead.
decimal Fee = 12345678.456M;
Fee.ToString("N2"); // 12,345,678.46 (Only an example)
Fee.ToString("F2"); // 12345678.46 (may also be 12345678,46, depending on culture)
See MSDN for more.
There is really no point to try to replace thousand separators using string.Replace, because it can be different on different computers.
You shouldn't use fee.ToString("N2") if you don't want the thousands separator in the first place. Simply use fee.ToString() to get it in the format you want.
Related
... but returns 12345?
The doc for Single.Parse says:
Exceptions
...
FormatException
s does not represent a numeric value.
...
For my understanding "123,45" doesn't represent a proper numeric value (in countries that use comma as thousands separator).
The system's CultureInfo has:
CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == "."
CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator == ","
CultureInfo.CurrentCulture.NumberFormat.NumberGroupSizes == [3]
Apparently the comma is simply ignored and this leads to even more irritating results: "123,45.67" or "1,23,45.67"–which look utterly wrong–become 12345.67.
Supplementary question
I don't get what this sentence in the doc is supposed to mean and whether this is relevant for this case:
If a separator is encountered in the s parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator.
In the default and US culture, the comma (,) is legal as a separator between groups. Think of larger numbers like this:
987,654,321
That it's in the wrong place for a group doesn't really matter; the parser isn't that smart. It just ignores the separator.
For the supplemental question, some cultures use commas as the decimal separator, rather than a group separator. This part of the documentation clarifies what will happen if the group separator and decimal separator are somehow set to the same character.
As Joel said, "the parser isn't that smart". The source code is available, so here's the proof.
The code for Single.Parse ends up calling Number.ParseNumber.
Interestingly, Number.ParseNumber is given a NumberFormatInfo object, which does have a NumberGroupSizes property, which defines "the number of digits in each group to the left of the decimal".
However, you'll notice that on line 851, where it checks for the group separator, it doesn't bother to reference the NumberGroupSizes property to check if the group separator is in an expected position. In fact Number.ParseNumber never uses the NumberGroupSizes property.
NumberFormatInfo.NumberGroupSizes is only ever used when converting a number to a string.
Why is it when I'm parsing a decimal (0) ToString my string shows as empty when using the method:
String somthing = someDecimal.ToString("#")
And when I'm using:
String somthing = somDecimal.ToString("0.##")
The string shows up as 0?
When I'm looking at the value in the debug mode in both way it's says they have a "0" in them.
From The "#" Custom Specifier
Note that this specifier never displays a zero that is not a
significant digit, even if zero is the only digit in the string. It
will display zero only if it is a significant digit in the number that
is being displayed.
If you want to display digits after your decimal point, you need to use 0.00 instead of 0.##.
Because pound "#" means convert to symbol if there is a number. 0 is an "empty" number, so it converts to "".
In fact, in second case, you get 0, as you imply to show at least one digit before dot.
This all is by design convention of C# language.
MSDN: the "#" Custom Specifier
The "#" custom format specifier serves as a digit-placeholder symbol.
If the value that is being formatted has a digit in the position where
the "#" symbol appears in the format string, that digit is copied to
the result string. Otherwise, nothing is stored in that position in
the result string. Note that this specifier never displays a zero that
is not a significant digit, even if zero is the only digit in the
string. It will display zero only if it is a significant digit in the
number that is being displayed.
So if the decimal would be 1 instead of 0 it would be diplayed even with ToString("#").
If you want a fixed number of decimals after the decimal point, you need to use
String somthing = somDecimal.ToString("0.00")
In your example you use the # specifier which means 'put here a number if there is a meaningful number'.
It would work if someDecimal is 0.01
decimal somDecimal = 0.01m
String somthing = somDecimal.ToString("0.##");
but not if
decimal somDecimal = 0.01m
String somthing = somDecimal.ToString("0.#");
So always add a comma and have it to two decimal places, "F" nearly works but can't find the right solution
decimal = 1000.5
test.Text = decimal.ToString("F")
I've also tried:
String.Format("{0:#,###.##}", decimal);
I want to display as the string as 1,000.50
Try:
String.Format("{0:#,###.00}", decimalNumber);
See: Custom Numeric Format Strings
0 - Zero placeholder Replaces the zero with the corresponding digit if
one is present; otherwise, zero appears in the result string. More
information: The "0" Custom Specifier.
It is not going to round the numbers, it is just string formatting.
For culture insensitive formatting do:
String.Format(CultureInfo.InvariantCulture, "{0:#,###.00}", decimalNmber);
String.Format(CultureInfo.InvariantCulture, "{0:0,0.00}", decimal)
For more options see this link.
And here you can test this online.
Can't figure out how to implement this task:
I need to format a decimal like this
var formatted = String.Format("{0:G}%", numberOfDecimalType);
So, the result should be number formatted number with trailing % sign.
The problem is that I need to get this result using .ToString() method.
I've tried numberOfDecimalType.ToString("G%") and different other variants but it didn't help
Is it possible?
You can add literal characters by using a custom numeric format string:
var formatted = numberOfDecimalType.ToString(#"#,##0.##\%");
The backslash ensures that % is treated as a literal character rather than a special formatting instruction.
Note, though, that this is not an exact replacement of G, i.e., you might need to adjust the number of #s to accommodate for the required number of decimal places.
You won't be able to append the "%" to the end using a single ToString call; you'll need to format the decimal using ToString and then append the percent sign separately:
var formatted = numberOfDecimalType.ToString("G") + "%";
You could simply append the percent, couldn't you?
formatted = numberOfDecimalType.ToString("G") + "%";
IF i have the following:
MyString.ValueType = typeof(System.Decimal);
how can i make this have an output of decimals with commas? In other words, instead of seeing 1234.5, i'd like to see 1,234.5
Use:
String output = MyString.ValueType.ToString("N");
The "N" format specifier will put in the thousands separators (,). For details, see Decimal.ToString(string).
Edit:
The above will use your current culture settings, so the thousands separators will depend on the current locale. However, if you want it to always use comma, and period for the decimal separator, you can do:
String output = MyString.ValueType.ToString("N", CultureInfo.InvariantCulture);
That will force it to use the InvariantCulture, which uses comma for thousands and period for decimal separation, which means you'll always see "1,234.5".