Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use double.Parse that if Null value, then value replace with 0

Tags:

c#

parsing

double

I'm trying to convert a string to a double. If it encounters null value the value should then be 0 (zero). However, it comes up with an error message:

"Operator '??' cannot be applied to operands of type 'double' and 'double'. It's sort of confusing because both numbers are double? How do I fix this?

double final = double.Parse(min.ToString()) ?? 0.0;
like image 454
Bjorn Avatar asked Mar 12 '26 19:03

Bjorn


1 Answers

Maybe this, assuming min is a string:

double final = double.Parse(min ?? "0");

Or perhaps:

double final = (min == null) ? 0 : double.Parse(min);

EDIT

Even better:

double final = Convert.ToDouble(min);

Per the documentation, that method will return

A double-precision floating-point number that is equivalent to the number in value, or 0 (zero) if value is null.

like image 88
user94559 Avatar answered Mar 15 '26 08:03

user94559