c# - How to return a double or string for a method -
i have method:
public static double c(string val) { return math.round(convert.todouble(val), 4); }
where pass in string, , if double, want round return double, if string, want return string is. of parameters pass in strings start, how determine if string or double, , how rewrite methoed have return type flexible enough return either?
ideally, use type variant in vba, dont think there analog in c#.
the following try parse double
. if returns true
put double in output variable output
; else return false
means val
isn't double , should use string val
instead.
public static bool c(string val, out double output) { if (double.tryparse(val, out output)) { output = math.round(output, 4); return true; } else { output = 0; return false; } }
use this:
string val = "123.45678"; double output; if ( c(val, out output) ) { // use double output } else { // val isn't double, use val directly }
Comments
Post a Comment