### Author Topic: Convert string to non-negative, non-zero double with default value  (Read 189 times)

0 Members and 1 Guest are viewing this topic.

#### Keith™

• Villiage Idiot
• Seagull
• Posts: 16614
• Superior Stupidity at its best
##### Convert string to non-negative, non-zero double with default value
« on: June 17, 2017, 12:16:33 pm »
I needed something like this .. comments and suggestions are welcome.

Code - C#: [Select]
`public double toPositiveDouble(string input, double def = 1){    double dv;    if (!Double.TryParse(input, out dv){        //can't parse, return default        dv = def;    }    else if ((dv + Math.Abs(dv)) == 0){        //value was negative, return default        dv = def;    }}`
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

#### Atook

• Swamp Rat
• Posts: 824
##### Re: Convert string to non-negative, non-zero double with default value
« Reply #1 on: June 17, 2017, 12:43:02 pm »
Was there a reason for the Abs call instead of checking for <0?

Here's my take, the extension method might be considered bad form.
Code - C#: [Select]
`public static double ToPositiveDouble(this string input, double def = 1){  double dv;  if (!Double.TryParse(input, out dv)){    //can't parse, return default    return def;  }  else if (dv<0)  {    //value was negative, return default    dv = def;  }  return dv;} // usagestring d = "-36";double dv = d.ToPositiveDouble(42.0);  `
« Last Edit: June 17, 2017, 12:46:51 pm by Atook »

#### Keith™

• Villiage Idiot
• Seagull
• Posts: 16614
• Superior Stupidity at its best
##### Re: Convert string to non-negative, non-zero double with default value
« Reply #2 on: June 17, 2017, 01:09:41 pm »
No, just a brain fart
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

#### Atook

• Swamp Rat
• Posts: 824
##### Re: Convert string to non-negative, non-zero double with default value
« Reply #3 on: June 17, 2017, 02:50:37 pm »
No, just a brain fart
Don't feel too bad, I hadn't had my coffee yet, realized I couldn't figure out how to check if a number was negative and google lead me here...

That's when I remembered my rule of no code before coffee..

#### kdub

• SuperMod
• Swamp Rat
• Posts: 925
• class keyThumper<T>:ILazy<T>
##### Re: Convert string to non-negative, non-zero double with default value
« Reply #4 on: June 17, 2017, 03:46:32 pm »

Code - C#: [Select]
`void Main() {	Console.WriteLine(IsNegative(-3.7));	Console.WriteLine(IsNegative(42)); 	//-------	string ds = "36";	double dv = ToPositiveDouble(ds, 42.0);	Console.WriteLine(\$"{ds} : =>{dv}");	//-------	ds = "-36";	dv = ToPositiveDouble(ds, 42.0);	Console.WriteLine(\$"{ds} : =>{dv}"); 	//-------	ds = "";	dv = ToPositiveDouble(ds, 42.0);	Console.WriteLine(\$"{ds} : =>{dv}"); 	//-------	ds = "0";	dv = ToPositiveDouble(ds, 42.0);	Console.WriteLine(\$"{ds} : =>{dv}");} public static double ToPositiveDouble(string input, double def = 1) {	double dv;		return (Double.TryParse(input, out dv) && !IsNegative(dv)) ? dv : def; } /// Zero is considered negative in this worldpublic static bool IsNegative<T>(T value)   where T : struct, IComparable<T> {	return value.CompareTo(default(T)) <= 0;}   `

True
False
36 : =>36
-36 : =>42
: =>42
0 : =>42

Revised : /// Zero is considered negative in this world

« Last Edit: June 17, 2017, 04:16:15 pm by kdub »
called Kerry in my other life

Sometimes the question is more important than the answer.

#### kdub

• SuperMod
• Swamp Rat
• Posts: 925
• class keyThumper<T>:ILazy<T>
##### Re: Convert string to non-negative, non-zero double with default value
« Reply #5 on: June 17, 2017, 04:37:52 pm »
It bugs me to consider 0 either positive or negative.

A better algorithm checks for IsPositive()  ... and it also read better.

Code - C#: [Select]
`void Main() {	Console.WriteLine(IsPositive(-3.7));	Console.WriteLine(IsPositive(42));	Console.WriteLine(IsPositive(0.0));	//-------	string ds = "36";	double dv = ToPositiveDouble(ds, 42.0);	Console.WriteLine(\$"{ds} : =>{dv}");	//-------	ds = "-36";	dv = ToPositiveDouble(ds, 42.0);	Console.WriteLine(\$"{ds} : =>{dv}");	//-------	ds = "";	dv = ToPositiveDouble(ds, 42.0);	Console.WriteLine(\$"{ds} : =>{dv}");	//-------	ds = "0";	dv = ToPositiveDouble(ds, 42.0);	Console.WriteLine(\$"{ds} : =>{dv}");} public static double ToPositiveDouble(string input, double def = 1) {	double dv;		return (Double.TryParse(input, out dv) && IsPositive(dv)) ? dv : def;}public static bool IsPositive<T>(T value)		where T : struct, IComparable<T> {	return value.CompareTo(default(T)) > 0;} public static bool IsNegative<T>(T value)   where T : struct, IComparable<T> {	return value.CompareTo(default(T)) < 0;} `

False
True
False
36 : =>36
-36 : =>42
: =>42
0 : =>42

« Last Edit: June 17, 2017, 04:42:41 pm by kdub »
called Kerry in my other life

Sometimes the question is more important than the answer.

#### Keith™

• Villiage Idiot
• Seagull
• Posts: 16614
• Superior Stupidity at its best
##### Re: Convert string to non-negative, non-zero double with default value
« Reply #6 on: June 18, 2017, 08:45:34 am »
Interesting. I had considered writing something like that, but I'd rather not rely on the short circuiting during the first of multiple comparisons. I know that it is supposed to always work, but it kinda feels like a hack instead of a designed feature.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

#### gile

• Water Moccasin
• Posts: 2075
• Marseille, France
##### Re: Convert string to non-negative, non-zero double with default value
« Reply #7 on: June 18, 2017, 10:40:17 am »
Hi,

Short circuiting IS a designed feature of the && operator and you can absolutely rely on it even with the out parameter.
Speaking English as a French Frog

#### kdub

• SuperMod
• Swamp Rat
• Posts: 925
• class keyThumper<T>:ILazy<T>
##### Re: Convert string to non-negative, non-zero double with default value
« Reply #8 on: June 18, 2017, 09:32:19 pm »
Hi,

Short circuiting IS a designed feature of the && operator and you can absolutely rely on it even with the out parameter.

Yes, If that was changed there'd be a lot of code around the world exploding
The link is void, so try
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-and-operator

called Kerry in my other life

Sometimes the question is more important than the answer.

#### Keith™

• Villiage Idiot
• Seagull
• Posts: 16614
• Superior Stupidity at its best
##### Re: Convert string to non-negative, non-zero double with default value
« Reply #9 on: June 18, 2017, 10:43:40 pm »
I've read that before. I dunno ... it may certainly be the design intention, but it still feels like a hack to me.

On another note, this is the latest incarnation
Code - C#: [Select]
`public double ToPositiveDouble(string input, double def = 1){    double dv;    return (Double.TryParse(input, out dv) && dv>0) ? dv : def;}`
The way I see it, it does all of the requisite things and returns a positive value greater than 0 or the default value if the input cannot be converted to a positive value.
I really think I am spending WAY too much time on such a trivial function
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

#### kdub

• SuperMod
• Swamp Rat
• Posts: 925
• class keyThumper<T>:ILazy<T>
##### Re: Convert string to non-negative, non-zero double with default value
« Reply #10 on: June 18, 2017, 10:51:26 pm »

I don't consider being correct is trivial ... but I think I know what you're saying.
called Kerry in my other life

Sometimes the question is more important than the answer.