Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round double in two decimal places in 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)

like image 457
sanjeev40084 Avatar asked Mar 01 '10 17:03

sanjeev40084


People also ask

What is meant by 2f in C?

2f" tells the printf method to print a floating point value (the double, x, in this case) with 2 decimal places.

How do you round a float to two decimal places in C++?

Using the ceil() function to round to 2 decimal places in C++ The ceil() function returns the smallest integer greater than the given integer. It will round up to the nearest integer. We can use this function to round to 2 decimal places in C++.


2 Answers

This works:

inputValue = Math.Round(inputValue, 2); 
like image 106
Alex LE Avatar answered Oct 02 '22 11:10

Alex LE


Math.Round(inputValue, 2, MidpointRounding.AwayFromZero) 
like image 21
nandin Avatar answered Oct 02 '22 12:10

nandin