Click here to Skip to main content
15,889,735 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My double number:

double inputValue = 48.4866666;

I want: inputValue = 48.48

inputValue = Math.Round(inputValue, 2);

What I have tried:

inputValue = Math.Round(inputValue, 2); but its give 48.49 and I need 48.48
Posted
Updated 4-Apr-17 21:46pm
v2

check this similar thread with Solved Answers
c# - Get Last 2 Decimal Places with No Rounding [^]
 
Share this answer
 
Use Math.Truncate Method (Double) (System)[^]
C#
inputValue = Math.Truncate(inputNumber * 100.0) / 100.0;
 
Share this answer
 
Round(Double, Int32) is rounding to nearest. If you need a different rounding mode use Math.Round Method (Double, Int32, MidpointRounding) (System)[^].
In your case:
C#
inputValue = Math.Round(inputValue, 2, MidpointRounding.AwayFromZero);

If you know that the input value is always valid and positive, you may also add half of the rounding position value:
C#
inputValue = Math.Round(inputValue + 0.005, 2);
For negative numbers you have to subtract.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900