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
Related
In my database, I have three point floating numbers columns and a process that multiplies the two first ones to get the third one value
item.ValorTotal = Math.Round(item.Qtde * item.ValorUnitario, 2, MidpointRounding.AwayFromZero);
but if my item.Qtde is like 0.03 and my item.ValorUnitario is 0.02, item.ValorTotal the result is 0.0006 and the variable receives zero because of the round, how do I can round to get 0.01 and continue using two numbers after decimal point?
In short I do like to round to the first possible number (0.01) when I receive a lower number like 0.006 or 0.0006
Instead of Math.Round() you can use Math.Ceiling().
The Math.Ceiling() method in C# is used to return the smallest integral value greater than or equal to the specified number.
So in your code example it will be something like:
item.ValorTotal = (Math.Ceiling((item.Qtde * item.ValorUnitario) * 100) / 100);
Output:
0,0006 => 0,01
0,0106 => 0,02
AwayFromZero doesn't mean that you always round upward. In real it works at most values like usual rounding. As far as I understood it only has an effect if you would round 0.005x.
Therefore write
item.ValorTotal = Math.Round(item.Qtde * item.ValorUnitario + 0.005, 2, MidpointRounding.AwayFromZero);
when you want to round upward and both values are positive.
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.
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#?
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).
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?