I am calculating the average of some values. Everything works fine.
What I want to do is to round the double to the 2nd decimal place.
e.g.
I would have 0.833333333333333333 displayed as
0.83
Is there anyway to do this?
Round the double itself like:
Math.Round(0.83333, 2, MidpointRounding.AwayFromZero);
(You should define MidpointRounding.AwayAwayFromZero to get the correct results. Default this function uses bankers rounding. read more about bankers rounding: http://www.xbeat.net/vbspeed/i_BankersRounding.htm so you can see why this won't give you the right results)
Or just the display value for two decimals:
myDouble.ToString("F");
Or for any decimals determined by the number of #
myDouble.ToString("#.##")
You say displays as - so that would be:
var d = value.ToString("f2");
See Standard numeric format strings
If you actually want to adjust the value down to 2dp then you can do what #middelpat has suggested.
Use
Math.Round(decimal d,int decimals);
as
Math.Round(0.833333333,2);
This will give you the result 0.83.
double d = 0.833333333333333333;
Math.Round(d, 2).ToString();
Try
decimalVar = 0.833333333333333333;
decimalVar.ToString ("#.##");
If you want to see 0.85781.. as 0.85, the easiest way is to multiply by 100, cast to int and divide by 100.
int val = (int)(0.833333333333333333 * 100);
var result = val /100;
It should produce the result you're looking for.
Related
I have the need to round a number for pricing sort of strangely as follows:
the value of an incoming price will be 3 decimal places (ie. 10.333)
It is necessary to round the first decimal place up if any number past said first decimal place is greater than 0.
for example:
10.300 = 10.3,
10.301 = 10.4,
10.333 = 10.4
before I go creating a custom method to do this I was wondering if anyone was aware of an existing usage/overload of Math.Round() or other already existing package to get this desired result?
Math.Round has an overload that accepts a MidpointRounding enum value, which lets you specify the strategy for rounding.
In your case, you always want to round up, which is called ToPositiveInfinity.
Math.Round(yourValue, 1, System.MidpointRounding.ToPositiveInfinity)
Approach with Math.Ceiling()
decimal input = 10.300m;
decimal result = Math.Ceiling(input * 10m) / 10m;
https://dotnetfiddle.net/Vck4Oa
double d = 10.300;
var result= Math.Ceiling((decimal)d * 10) / 10;
here is what I'm trying to do:
double result = Math.Pow((1 + 8), 60) - 1;
And the result variable is:
1.7970102999144311E+57 double
And trying to:
Math.Round(result, 5);
Returns same : 1.7970102999144311E+57 double
I'd like to round it to 1.79701 for example
Any solutions ?
You're misunderstanding what you're seeing.
1.7970102999144311E+57
Is scientific notation for
1797010299914431100000... (with 41 trailing zeros).
It is a whole number, thus rounding it to 5 decimal places will correctly return the same value.
What you want to do is format the output of the number
String.Format(CultureInfo.InvariantCulture, "{0:0.#####E+0}", result);
Which returns 1.79701E+57. Note that this is a very different number from 1.79701
The problem you're having is that Math.Round rounds things to the right of the decimal point. For example if you're dealing with currency and you perform an operation that leaves you with $1.5234524, you would use:
Math.Round(1.5234524,2);
// output 1.52
The number you're dealing with is actually scientific notation for a very large number with nothing to the right of the decimal point. This is why the result of Math.Round is the same as the input.
The earlier comments and answers are correct. But to get what you are trying to achieve you can use the following:
double result = Math.Pow((1 + 8), 60) - 1;
string s = String.Format("{0:E5}", result);
double d = Double.Parse(s);
I want to round a decimal number with 2 places to an integer.
Ex: 3.772.002,47 --- decimal
3.772.003
Im using this code but with no success:
ssOut = Math.Round(ssIn, ssDecimal, MidpointRounding.AwayFromZero);
Anyone can help me?
Thanks.
You need cast a result to int:
var ssOut = (int)Math.Round(ssIn,
0, /* zero because you want to cast to integer - no fraction part is needed */
MidpointRounding.AwayFromZero);
You can do the same with the following overload:
var ssOut = (int)Math.Round(ssIn, MidpointRounding.AwayFromZero);
But it looks like you need Math.Ceiling - like it is said in comments below questions.
Looks like your input may be a string. First use Float.Parse() or Double.Parse() to convert string to number before rounding.
I have been searching forever and I simply cannot find the answer, none of them will work properly.
I want to turn a double like 0.33333333333 into 0,33 or 0.6666666666 into 0,66
Number like 0.9999999999 should become 1 though.
I tried various methods like
value.ToString("##.##", System.Globalization.CultureInfo.InvariantCulture)
It just returns garbage or rounds the number wrongly.
Any help please?
Basically every number is divided by 9, then it needs to be displayed with 2 decimal places without any rounding.
I have found a nice function that seems to work well with numbers up to 9.999999999
Beyond that it starts to lose one decimal number. With a number like 200.33333333333
its going to just display 200 instead of 200,33. Any fix for that guys?
Here it is:
string Truncate(double value, int precision)
{
string result = value.ToString();
int dot = result.IndexOf(',');
if (dot < 0)
{
return result;
}
int newLength = dot + precision + 1;
if (newLength == dot + 1)
{
newLength--;
}
if (newLength > result.Length)
{
newLength = result.Length;
}
return result.Substring(0, newLength);
}
Have you tried
Math.Round(0.33333333333, 2);
Update*
If you don't want the decimal rounded another thing you can do is change the double to a string and then get get a substring to two decimal places and convert it back to a double.
doubleString = double.toString();
if(doubleString.IndexOf(',') > -1)
{
doubleString = doubleString.Substring(0,doubleString.IndexOf(',')+3);
}
double = Convert.ToDouble(doubleString);
You can use a if statement to check for .99 and change it to 1 for that case.
Math.Truncate(value * 100)/100
Although I make no guarantees about how the division will affect the floating point number. Decimal numbers can often not be represented exactly in floating point, because they are stored as base 2, not base 10, so if you want to guarantee reliability, use a decimal, not a double.
Math.Round((decimal)number, 2)
Casting to a decimal first will avoid the precision issues discussed on the documentation page.
Math.Floor effectively drops anything after the decimal point. If you want to save two digits, do the glitch operation - multiply then divide:
Math.Floor(100 * number) / 100
This is faster and safer than doing a culture-dependent search for a comma in a double-converted-to-string, as accepted answer suggests.
you can try one from below.there are many way for this.
1.
value=Math.Round(123.4567, 2, MidpointRounding.AwayFromZero) //"123.46"
2.
inputvalue=Math.Round(123.4567, 2) //"123.46"
3.
String.Format("{0:0.00}", 123.4567); // "123.46"
4.
string.Format("{0:F2}", 123.456789); //123.46
string.Format("{0:F3}", 123.456789); //123.457
string.Format("{0:F4}", 123.456789); //123.4568
I have this number: 1234.5678 (as a text)
I need this number as double, with only 2 numbers after the dot
But without Round the number
in 1234.5678 - i get 1234.57
in 12.899999 - i get 12.90
How I can do it ?
You should be able to get what you want like this:
number.ToString("#0.00")
Multiply by 100, take floor() of the number, divide by 100 again.
You didn't post the results you wanted, but I assume you want truncation, so that you'll see 1234.56, and 12.89. Try:
decimal d = 1234.89999M;
Console.WriteLine(Math.Truncate(d * 100) / 100);
This is because you can't represent these numbers exactly as doubles, so converting, rounding, and then reprinting as text results in a loss of precision.
Use 'decimal' instead.
This should do the trick.
string rawVal = "1234.5678";
System.Math.Floor((double.parse(rawVal)) * 100) / 100;
Take this floating point arithmetic!
var num = "1234.5678";
var ans = String.Empty;
if( !String.IsNullOrEmpty(num) && num.Contains('.') ) // per comment
{
ans = num.Substring(0, num.IndexOf('.') + 3);
}
This doesn't work?
double d = 1234.5678;
double rounded =Math.Round(d, 2);