user2818430 user2818430 - 1 year ago 88
C# Question

c# decimal keep only 2 decimals

I have a decimal number: 12.4465463455

decimal value = 12.4465463455m;


How can I round this decimal and remove extra decimals but 2 so the decimal will be
12.45
? Please notice I don't need a string. I know how to convert this to a string with 2 decimals and rounded. But have no idea how to manipulate the decimal variable itself.

Answer Source

Just use Math.Round, so:

decimal value = 12.4465463455m;
decimal value2dp = Math.Round(value, 2); //2 => 2 decimal places
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download