Putting Commas in a Decimal - c#

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

Related

How to convert 2.3449 to Decimal

I am trying to convert 2.3449 to Decimal but It converts like 23449,0
I am converting it like below
string temp = "2.3449";
decimal value_ = Convert.ToDecimal(temp);
if I replace the DOT with COLON, it converts it perfectly. But, I dont want to Replace the DOT with COLON in string. What is the good way of converting without replacing.
Your culture treats . as the thousands separator, rather than decimal separator.
You can always use a specific culture:
var val = decimal.Parse(temp, CultureInfo.InvariantCulture);
The same goes for ToString - if you want to print the number with . as the decimal separator, just use the appropriate culture. The local culture (the default) is usually the best bet for anything user-facing, though. Invariant culture is perfect for persistence :)

c# - How do I round a decimal value to 2 decimal places. with , every 1000

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 remove white spaces from a string?

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.

Culture info comma doesn't read my decimal as decimal

The value of my_waste in db which I enter with comma is:
16.78
I make a selection via linq and:
res.Add("testdb", p.my_waste);
and i get 1678.
I tried:
res.Add("test", double.Parse(p.my_waste.ToString(), CultureInfo.InvariantCulture));
and
res.Add("test", string.Format(CultureInfo.InvariantCulture, "{0:0.00}", p.my_waste));
And I still got 1678.
From the MSDN
The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region.
This means that it expects numbers to have a decimal point as English (all variants) uses a decimal point not a decimal comma.
If you are inputting data with a decimal comma you need to use a culture (e.g. French) that has a decimal comma to to the string <-> number conversion.
At the moment the comma is being treated as a thousands separator and is effectively ignored, so "16,78" comes out as 1678 as you have observed.

Why string.Format produces such result?

I have a line like the following in my code:
string buffer = string.Format(CultureInfo.InvariantCulture, "{0:N4}", 1008.0);
Why does buffer contain 1,008.0 (note comma) after executing this line?
Yes, I do guess that it's caused by my regional settings. The question is why they affect the result in this case?
EDIT:
Ok, I understand that it's completely my fault. It seems like I should have used F format specifier.
The InvariantCulture is loosely based on en-US which uses , as a thousands (group) separator.
Your result is what I would expect.
I also point you to the details of the N numeric format specifier:
The numeric ("N") format specifier converts a number to a string of the form "-d,ddd,ddd.ddd…", where "-" indicates a negative number symbol if required, "d" indicates a digit (0-9), "," indicates a group separator, and "." indicates a decimal point symbol.
You're using the invariant culture; your culture is irrelevant to this. For this, the N4 format means
-d,ddd,ddd,ddd...
That is, possible leading negative sign indicator and commas between thousands groups. For details see: http://msdn.microsoft.com/en-us/library/dwhawy9k#NFormatString
You can look at
NegativeSign
NumberNegativePattern
NumberGroupSizes
NumberGroupSeparator
NumberDecimalSeparator
NumberDecimalDigits
for the invariant culture. If you do, you'll see:
-
1
{ 3 }
,
.
2
You are getting the comma because of "{0:N4}"
n ----- Number with commas for thousands ----- {0:n}
Source:
You will get the comma even without specifying InvariantCulture
Console.WriteLine(string.Format("{0:n4}", 1008.0));

Categories

Resources