c# decimal keep only 2 decimals - c#

I have a decimal number: 12.4465463455
decimal value = 12.4465463455m;
How can I round this decimal and remove extra decimals but 2 so the decimal will be 12.45 ? Please notice I don't need a string. I know how to convert this to a string with 2 decimals and rounded. But have no idea how to manipulate the decimal variable itself.

Just use Math.Round, so:
decimal value = 12.4465463455m;
decimal value2dp = Math.Round(value, 2); //2 => 2 decimal places

Related

Format a number value with minimal decimal points

I need to format a number (decimal) into a string with minimal decimal points.
for example, let's say the minimal decimal point is 3
123.123654 => 123.123654
123.12 => 123.120
123.1 => 123.100
123 => 123.000
What is the best way to achieve this result?
If you use really a decimal the decimal places are preserved, so you can write:
decimal d = 123.120m;
Console.WriteLine(d); // 123.120
If you can't do this you can always provide a format with ToString:
Console.WriteLine(d.ToString("N3"));
Reading: Standard numeric format strings, especially. numeric format specifier
As juharr pointed out this shows just 3 decimal places. You can use string.Format:
string result = string.Format("{0:0.000##################}", d);

Wrong parsing from string to double

double value = double.Parse("4655.927411110702", CultureInfo.InvariantCulture);
Why this parsing is getting me result: 4655.9274111107025?
Somehow it adds to my number 5 at the end. How should I convert this string to double and have a correct result?
You can't. Not all numbers can be represented exactly in double precision floating point, and the closest such double to 4655.927411110702 is 4655.927411110702450969256460666656494140625, and the default formatting you're using trims off the majority of the "joke" digits.
C# does have a decimal type. Can you not use that?
decimal value = decimal.Parse("4655.927411110702", CultureInfo.InvariantCulture);

c# how to prevent double value truncation when converting to string

Double x = 11.123456789123456;
string y = Convert.ToString(x);
//gives y=11.1234567891235
//y should be =11.123456789123456
From the above code how can I prevent the last digit(6) from being truncated
Use
string y = x.ToString("G17");
or
string y = x.ToString("R");
as written here:
By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.
Note that not all the numbers can be represented exactly...
11.123456789123458.ToString("G17") == "11.123456789123457"
double is only precise up to 15-16 digits, try using decimal type
See Msdn
Decimal
The reason this is happening is because Double occupies 8 bytes and has precision of 15-16 digits.
Use Decimal instead
Decimal x = 11.123456789123456M;
string y = Convert.ToString(x);
//gives y=11.12345678912356
Refer this link, look for answer by cds333

Formatting a decimal value with ToString() to have commas as thousand separators where the amount of decimal places is unknown

I have many decimals, each rounded differently:
decimal quantity = Decimal.Round(item.Quantity.Value, 2,
MidpointRounding.AwayFromZero);
decimal difference = Decimal.Round(quantity * eva, 0,
MidpointRounding.AwayFromZero);
When binding to the UI, I convert to string like this:
string Quantity = quantity.ToString("G", CultureInfo.InvariantCulture);
string Difference = difference.ToString("G", CultureInfo.InvariantCulture);
Is there a generic way to insert commas for thousand separators while keeping the original decimal rounding the same?
Try using Format.
double d = 1.234567;
String output = d.ToString("#,##0.##");
Also,
double d = 123456789.1;
string format = d.ToString().
IndexOf(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
>=0 ? "#,##0.00" : "#,##0";
Console.WriteLine (d.ToString(format));
For anyone wondering, I ended up using String.Format(new CultureInfo("en-US"), "{0:N}", difference) and changed the N depending on how many decimal places I needed.
You can use the "N" format specifier and supply the number of digits you want any number to retain. If you want each number to potentially have a different number of digits you wall have to determine the number to supply to the format string each time.
quantity.ToString("N(digits)");
Complete documentation is at http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#NFormatString

convert decimal to a string - in thousands with rounding

I need to convert a decimal to currency string so i did this:
CultureInfo usa = new CultureInfo("en-US");
NumberFormatInfo nfi = usa.NumberFormat;
nfi.CurrencyDecimalDigits = 0;
myValueFormated = String.Format(nfi, "{0:C}", value);
It removed decimal places, gave me a comma separator for thousands and and currency symbol.
But I also need to display that number in thousands, rounded.
Any ideas?
Thanks
You need to do the rounding bit yourself:
value = Math.Round(value / 1000);

Categories

Resources