Round to places to 0 c# - c#

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.

Related

Is there a C# function or overload to Math.Round that will allow abnormal rounding practices?

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;

Rounding Off a double value using Math.Round

I have a double value: 0.314285 which I want to Round off to 5 decimal places. From a mathematical point of view my expectant result is: 0.31429. In my code I use the Math.Round with MidPointRounding.AwayFromZero parameter overload, the resultant output being: 0.31428.
Is there another way to implement to have the output result as: 0.31429??
You should read the rounding and precision article. The real representation of your number in memory can be something like 0.3142849999999999, and therefore you are getting 0.31428 result. Using a decimal type can help to solve this issue
var value = 0.314285m;
var result = Math.Round(value, 5, MidpointRounding.AwayFromZero); //0.31429
public static double RoundUp(double i, int decimalPlaces)
{
var power = Math.Pow(10, decimalPlaces);
return Math.Ceiling(i * power) / power;
}
RoundDown(0.314285, 5); //0.31429
Math.Round(decimal number to round, int number of decimal places)
I think this should work for what you are trying to do.

Format a double to two digits after the comma without rounding up or down

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

Round to a certain decimal place

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.

How to round a decimal for output?

Using C#, I want to format a decimal to only display two decimal places and then I will take that decimal and subtract it to another decimal. I would like to be able to do this without having to turn it into a string first to format and then convert it back to a decimal. I'm sorry I forget to specify this but I don't want to round, I just want to chop off the last decimal point. Is there a way to do this?
If you don't want to round the decimal, you can use Decimal.Truncate. Unfortunately, it can only truncate ALL of the decimals. To solve this, you could multiply by 100, truncate and divide by 100, like this:
decimal d = ...;
d = Decimal.Truncate(d * 100) / 100;
And you could create an extension method if you are doing it enough times
public static class DecimalExtensions
{
public static decimal TruncateDecimal(this decimal #this, int places)
{
int multipler = (int)Math.Pow(10, places);
return Decimal.Truncate(#this * multipler) / multipler;
}
}
You can use: Math.Round(number,2); to round a number to two decimal places.
See this specific overload of Math.Round for examples.
Math.Round Method (Decimal, Int32)
You don't want to format it then, but to round it. Try the Math.Round function.
Take a look at Math.Round

Categories