### Author Topic: Evolution of IsIntValue()  (Read 614 times)

0 Members and 1 Guest are viewing this topic.

#### kdub_nz

• Mesozoic keyThumper
• SuperMod
• Water Moccasin
• Posts: 2162
• class keyThumper<T>:ILazy<T>
##### Evolution of IsIntValue()
« on: February 10, 2024, 06:19:50 PM »
This was the evolution of the Extension Method.

If anyone has a cleaner result I'd like to know.

Code - C#: [Select]
1. //double val = 42.00;
2. double val = 42.42;
3.
4. Console.WriteLine(\$"{Math.Round(val, 4 , MidpointRounding.AwayFromZero):0.000000}") ;
5. Console.WriteLine((int)Math.Round(val, 4 , MidpointRounding.AwayFromZero)) ;
6.
7. Console.WriteLine(Math.Abs( val - (int) Math.Round(val, MidpointRounding.AwayFromZero)));
8.
9. Console.WriteLine(1e-16 );
10.
11. Console.WriteLine(Math.Abs( val - (int)Math.Round(val, MidpointRounding.AwayFromZero))
12.                     < 1e-16
13.                  );
14.
15. Console.WriteLine(val.IsValueInt());
16.
17. string message = 50.00001.IsValueInt() ? " value is integral" : " value is NOT integral";
18.
19. Console.WriteLine(message);
20.
21.
22. static public class ExHelper
23. {
24.         static public bool IsValueInt(this double dvalue) =>
25.                 Math.Abs(dvalue - (int)Math.Round(dvalue, 4,
26.                                                         MidpointRounding.AwayFromZero))
27.                                                         < 1e-16;
28. }
29.

OUTPUT:
Code - C#: [Select]
1. 42.420000
2. 42
3. 0.4200000000000017
4. 1E-16
5. False
6. False
7.  value is NOT integral
8.
« Last Edit: February 10, 2024, 06:25:22 PM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
Sometimes the question is more important than the answer.

#### kdub_nz

• Mesozoic keyThumper
• SuperMod
• Water Moccasin
• Posts: 2162
• class keyThumper<T>:ILazy<T>
##### Re: Evolution of IsIntValue()
« Reply #1 on: February 11, 2024, 09:34:00 PM »
Something like this will probably suit me better.

Code - C#: [Select]
1.       [CommandMethod("Test0212")]
2.       public void Test0212()
3.       {
4.
5.          if (TryIsValueIntegral(101, out int a))
6.             WriteMessage("\na: " + a);
7.
8.          if (TryIsValueIntegral(11.42, out int b))
9.             WriteMessage("\nb: " + b);
10.
11.          if (TryIsValueIntegral("22", out int c))
12.             WriteMessage("\nc: " + c);
13.
14.          if (TryIsValueIntegral("22.42", out int d))
15.             WriteMessage("\nd: " + d);
16.
17.          if (TryIsValueIntegral(1111.00, out int e))
18.             WriteMessage("\ne: " + e);
19.
20.          if (TryIsValueIntegral("122.00", out int f))
21.             WriteMessage("\nf: " + f);
22.
23.       }
24.
25.       bool TryIsValueIntegral(double dvalue, out int ivalue)
26.       {
27.          ivalue = (int)Math.Round(dvalue, 4, MidpointRounding.AwayFromZero);
28.          return Math.Abs(dvalue - ivalue) < 1e-16;
29.       }
30.
31.       bool TryIsValueIntegral(string svalue, out int ivalue)
32.       {
33.          if (double.TryParse(svalue, out double cvalue))
34.          {
35.             ivalue = (int)Math.Round(cvalue, 4, MidpointRounding.AwayFromZero);
36.
37.             return Math.Abs(cvalue - ivalue) < 1e-16;
38.          }
39.          else
40.          {
41.             ivalue = 0;
42.             return false;
43.          }
44.       }
45.

Code - C#: [Select]
1. Command: TEST0212
2. a: 101
3. c: 22
4. e: 1111
5. f: 122
6.
7.

Now I just need to parse the resultBuffers ( tomorrow or next )

« Last Edit: February 12, 2024, 03:48:20 AM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---