Round double in two decimal places in C#? - c#

I want to round up double value in two decimal places in c# how can i do that?
double inputValue = 48.485;
after round up
inputValue = 48.49;
Related: c# - How do I round a decimal value to 2 decimal places (for output on a page)

This works:
inputValue = Math.Round(inputValue, 2);

Math.Round(inputValue, 2, MidpointRounding.AwayFromZero)

Another easy way is to use ToString with a parameter.
Example:
float d = 54.9700F;
string s = d.ToString("N2");
Console.WriteLine(s);
Result:
54.97

Use Math.Round
value = Math.Round(48.485, 2);

You should use
inputvalue=Math.Round(inputValue, 2, MidpointRounding.AwayFromZero)
Math.Round
Math.Round rounds a double-precision floating-point value to a
specified number of fractional digits.
MidpointRounding
Specifies how mathematical rounding methods should process a number
that is midway between two numbers.
Basically the function above will take your inputvalue and round it to 2 (or whichever number you specify) decimal places. With MidpointRounding.AwayFromZero when a number is halfway between two others, it is rounded toward the nearest number that is away from zero. There is also another option you can use that rounds towards the nearest even number.

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

Use an interpolated string, this generates a rounded up string:
var strlen = 6;
$"{48.485:F2}"
Output
"48.49"

I think all these answers are missing the question. The problem was to "Round UP", not just "Round". It is my understanding that Round Up means that ANY fractional value about a whole digit rounds up to the next WHOLE digit. ie: 48.0000000 = 48 but 25.00001 = 26. Is this not the definition of rounding up? (or have my past 60 years in accounting been misplaced?

Related

why Math.Round(66.665d, 2) is 66.67 in c# [duplicate]

The MidpointRound.AwayFromZero overload is supposed to round the 5 up in a rounding situation.
EX: Math.Round(1.5, 1, MidpointRounding.AwayFromZero);
This rounds 1.5 to 2.
However, when I try Math.Round(1.025, 2, MidpointRounding.AwayFromZero); it rounds it down to 1.02. It is supposed to round up to 1.03 though.
Does anyone have any idea why this is happening? Thanks!
The value 1.025 cannot be represented exactly as the type double. Floating point numbers have that problem. See Why are floating point numbers inaccurate?.
When you write 1.025 as a double number, it will internally be a little bit smaller than 1.025. That's why the rounding delivers an unexpected value.
When dealing with money or other stuff where you want the value be exactly the same as displayed, use the type decimal instead of double:
decimal roundedValue = Math.Round(1.025m, 2, MidpointRounding.AwayFromZero);
Note that the m after the number turns the number into a literal of the decimal type.

Rounding-up a decimal to 2 decimal places

I have a problem to round a number to two decimal places.
I have the number 3106.4647771976413339683766317M.
The correct round to two decimal places is 3106.47, but using Math.Round(value, 2, MidpointRounding.AwayFromZero) the number is 3106.46.
The problem is the method look to third decimal place to round, but if it look to fourth decimal place will generate the correct number.
Someone has something like that?
Mathematically, the correct round to two decimal places is 3106.46.
What you probably want is a ceiling:
Math.Ceiling(3106.4647771976413339683766317M * 100) / 100
produces 3106.47. There is no version of Math.Ceiling accepting a number of decimal places, that's why there are multiplication and division.
In addition, note that there is a caveat in this expression:
Math.Round(value, 2, MidpointRounding.AwayFromZero)
Math.Round does not have a variant with three arguments, where the first one is a decimal. It works, because the value is implicitly converted to double. However, this is unwanted.
If you really want to round that way (more of a common mistake than actual rounding in the traditional sense) you can round up from the furthest right digit and move left one place at a time.
double numberToRound = 3106.4647771976413339683766317;
int placesToRoundTo = 2;
int smallestPlaceIndex = 24; // you would need to determine this value
for (int i = smallestPlaceIndex; i >= placesToRoundTo)
{
numberToRound = Math.Round(numberToRound, i, MidpointRounding.AwayFromZero);
}
There isn't a simple built-in method to do this because it isn't normal rounding.

How to round with fractional digits in C#?

How do I round up two digits after the comma?
Ex: I have a 3,7512 . I need 3,76 .
Would that work for you?
double value = 0.01 * Math.Ceiling(100.0 * originalValue)
Use the Math.Round method. It accepts a second argument that determines how many fractional digits you want to round.
Math.Round(3.7512, 2) // this rounds up to 3.75
Math.Round(3.7572, 2) // this rounds up to 3.76

Rounding decimals to two decimal places in c#

I want to round 54.5345 to 54.54
that is if I have a third decimal place then i want to add 1 to the 2nd decimal place.
I have tried using math.round but it always rounds down if the third decimal is less than 5
try:
d = Math.Ceiling(d * 100) / 100;
where d is your decimal.
I think you should try this:
double a = Math.Round(-57.5345, 2);
This works for negative numbers too.
The way you are rounding off is not correct.
You can also refer:
How do you round a number to two decimal places in C#?

Casting int to double and rounding off to last whole number

I'm stuck with a little issue here, say you have the following code:
int whole = 0;
double decimal = 88.00
whole = decimal / 1.5; //now the answer is 58.66
So here's the issue, explicitly casting a double to an int is easy enough. But if I do this now 'whole' is going to be set to 59. This - is not so good, I want it to be set to the last whole number (being 58).
How do you do this in C#?
To round doubles to integers, you have 4 basic math functions:
Math.Round() - Rounds to the nearest whole number (or user specified number of deciml places), and lets you choose to round middle points up or down.
Math.Floor() - Rounds to the first whole number toward negative infinity.
Math.Ceiling() - Rounds to the first whole number toward positive infinity.
Math.Truncate() - Rounds to the first whole number toward zero.
I think you want either Floor or Truncate. Both round down for positive numbers, but Truncate rounds -3.6 to -3, while Floor rounds it to -4.
Casting to int does the same as truncating, so you can use that if you prefer.
Math.Floor:
whole = (int)Math.Floor(decimal / 1.5);
If you cast double to int, the answer will NOT be 59 -- it will be 58. When casting double to int, the value will be rounded towards zero. So, this is sufficient:
int whole = 0;
double x = 88.00;
whole = (int)(x / 1.5); // whole will be 58
Use Math.Floor if you want to round to the last whole number, and Math.Ceiling if you want to round to the next.
If you need rounding, it's pretty common to use:
whole = (int)(decimal / 1.5 + 0.5);
Without the 0.5, you're truncating, not rounding.
If you have a rounding function in your math libraries, that's good too. Some of these will do the odd/even thing for rounding 0.5 to avoid a little bit of data skew.
This will convert the double value into int:
whole = (int)(decimal / 1.5);
Also, you can use Math.Floor(doubleValue).

Categories