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

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
I needed something like this .. comments and suggestions are welcome.

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

Find me on https://parler.com @kblackie

Atook

  • Swamp Rat
  • Posts: 1027
  • AKA Tim
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]
  1. public static double ToPositiveDouble(this string input, double def = 1)
  2. {
  3.   double dv;
  4.   if (!Double.TryParse(input, out dv)){
  5.     //can't parse, return default
  6.     return def;
  7.   }
  8.   else if (dv<0)
  9.   {
  10.     //value was negative, return default
  11.     dv = def;
  12.   }
  13.   return dv;
  14. }
  15.  
  16. // usage
  17. string d = "-36";
  18. double dv = d.ToPositiveDouble(42.0);
  19.  
  20.  
« Last Edit: June 17, 2017, 12:46:51 PM by Atook »

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • 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

Find me on https://parler.com @kblackie

Atook

  • Swamp Rat
  • Posts: 1027
  • AKA Tim
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... 8-)

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

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • 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 »
How about :

Code - C#: [Select]
  1. void Main() {
  2.         Console.WriteLine(IsNegative(-3.7));
  3.         Console.WriteLine(IsNegative(42));
  4.  
  5.         //-------
  6.         string ds = "36";
  7.         double dv = ToPositiveDouble(ds, 42.0);
  8.         Console.WriteLine($"{ds} : =>{dv}");
  9.         //-------
  10.         ds = "-36";
  11.         dv = ToPositiveDouble(ds, 42.0);
  12.         Console.WriteLine($"{ds} : =>{dv}");
  13.  
  14.         //-------
  15.         ds = "";
  16.         dv = ToPositiveDouble(ds, 42.0);
  17.         Console.WriteLine($"{ds} : =>{dv}");
  18.  
  19.         //-------
  20.         ds = "0";
  21.         dv = ToPositiveDouble(ds, 42.0);
  22.         Console.WriteLine($"{ds} : =>{dv}");
  23. }
  24.  
  25. public static double ToPositiveDouble(string input, double def = 1) {
  26.         double dv;     
  27.         return (Double.TryParse(input, out dv) && !IsNegative(dv)) ? dv : def;
  28.  
  29. }
  30.  
  31. /// Zero is considered negative in this world
  32. public static bool IsNegative<T>(T value)
  33.    where T : struct, IComparable<T> {
  34.         return value.CompareTo(default(T)) <= 0;
  35. }
  36.  
  37.  
  38.  



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
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • 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]
  1. void Main() {
  2.         Console.WriteLine(IsPositive(-3.7));
  3.         Console.WriteLine(IsPositive(42));
  4.         Console.WriteLine(IsPositive(0.0));
  5.         //-------
  6.         string ds = "36";
  7.         double dv = ToPositiveDouble(ds, 42.0);
  8.         Console.WriteLine($"{ds} : =>{dv}");
  9.         //-------
  10.         ds = "-36";
  11.         dv = ToPositiveDouble(ds, 42.0);
  12.         Console.WriteLine($"{ds} : =>{dv}");
  13.         //-------
  14.         ds = "";
  15.         dv = ToPositiveDouble(ds, 42.0);
  16.         Console.WriteLine($"{ds} : =>{dv}");
  17.         //-------
  18.         ds = "0";
  19.         dv = ToPositiveDouble(ds, 42.0);
  20.         Console.WriteLine($"{ds} : =>{dv}");
  21. }
  22.  
  23. public static double ToPositiveDouble(string input, double def = 1) {
  24.         double dv;     
  25.         return (Double.TryParse(input, out dv) && IsPositive(dv)) ? dv : def;
  26. }
  27. public static bool IsPositive<T>(T value)
  28.                 where T : struct, IComparable<T> {
  29.         return value.CompareTo(default(T)) > 0;
  30. }
  31.  
  32. public static bool IsNegative<T>(T value)
  33.    where T : struct, IComparable<T> {
  34.         return value.CompareTo(default(T)) < 0;
  35. }
  36.  


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
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • 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

Find me on https://parler.com @kblackie

gile

  • Gator
  • Posts: 2507
  • 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_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • 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
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • 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]
  1. public double ToPositiveDouble(string input, double def = 1){
  2.     double dv;
  3.     return (Double.TryParse(input, out dv) && dv>0) ? dv : def;
  4. }
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  :2funny: :2funny: :2funny:
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2124
  • 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
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.