Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round nullable decimal to 5 decimal places

Tags:

.net

vol = Decimal.Round(exposure.Volatility, 5);

This won't let me round

exposure.Volatility

to a decimal b/c it say's conversion from decimal? to decimal cannot take place.

How do I round that number to 5 decimal places? It's a nullable decimal.

like image 547
slandau Avatar asked Jan 06 '11 18:01

slandau


1 Answers

Assuming a null value is equal to zero in this circumstance, something like:

vol = exposure.Volatility.HasValue ? Decimal.Round(exposure.Volatility.Value, 5) : 0;

If "vol" is also nullable then do:

vol = exposure.Volatility.HasValue ? Decimal.Round(exposure.Volatility.Value, 5) : null;
like image 169
Sapph Avatar answered Sep 18 '22 15:09

Sapph