Skip to content

Commit d646c12

Browse files
MikeStallMike Stall
and
Mike Stall
authored
add AsDouble() (#1906)
Add helpers to say: `double x = value.AsDouble();` or AsDecimal. this avoids having to cast and guess whether it's decimal or double. --------- Co-authored-by: Mike Stall <[email protected]>
1 parent d71f80e commit d646c12

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/libraries/Microsoft.PowerFx.Core/Public/Values/FormulaValue.cs

+63
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Diagnostics;
56
using System.Text;
67
using Microsoft.PowerFx.Core.IR;
@@ -88,6 +89,68 @@ public bool TryGetPrimitiveValue(out object val)
8889

8990
val = null;
9091
return false;
92+
}
93+
94+
public bool AsBoolean()
95+
{
96+
if (this is BlankValue)
97+
{
98+
return default;
99+
}
100+
101+
if (TryGetPrimitiveValue(out object val))
102+
{
103+
if (val is bool b)
104+
{
105+
return b;
106+
}
107+
}
108+
109+
throw new InvalidOperationException($"Can't coerce to double from {this.Type._type.GetKindString()})");
110+
}
111+
112+
public double AsDouble()
113+
{
114+
if (this is BlankValue)
115+
{
116+
return default;
117+
}
118+
119+
if (TryGetPrimitiveValue(out object val))
120+
{
121+
if (val is double d1)
122+
{
123+
return d1;
124+
}
125+
else if (val is decimal d2)
126+
{
127+
return (double)d2;
128+
}
129+
}
130+
131+
throw new InvalidOperationException($"Can't coerce to double from {this.Type._type.GetKindString()})");
132+
}
133+
134+
public decimal AsDecimal()
135+
{
136+
if (this is BlankValue)
137+
{
138+
return default;
139+
}
140+
141+
if (TryGetPrimitiveValue(out object val))
142+
{
143+
if (val is double d1)
144+
{
145+
return (decimal)d1;
146+
}
147+
else if (val is decimal d2)
148+
{
149+
return d2;
150+
}
151+
}
152+
153+
throw new InvalidOperationException($"Can't coerce to decimal from {this.Type._type.GetKindString()})");
91154
}
92155
}
93156
}

src/tests/Microsoft.PowerFx.Interpreter.Tests/ValueTests.cs

+46
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,52 @@ public void TryGetIntTest(double doubleValue, decimal decimalValue, string strin
423423

424424
Assert.False(Library.TryGetInt(str, out int outputStringToInt));
425425
}
426+
427+
[Fact]
428+
public void AsNumberTests()
429+
{
430+
NumberValue num = FormulaValue.New((double)1.2);
431+
DecimalValue dec = FormulaValue.New(1.2m);
432+
433+
Assert.Equal(1.2, num.AsDouble());
434+
Assert.Equal(1.2, dec.AsDouble());
435+
436+
Assert.Equal(1.2m, num.AsDecimal());
437+
Assert.Equal(1.2m, dec.AsDecimal());
438+
}
439+
440+
[Fact]
441+
public void AsNumberTestsBlank()
442+
{
443+
BlankValue num = FormulaValue.NewBlank(FormulaType.Number);
444+
BlankValue dec = FormulaValue.NewBlank(FormulaType.Decimal);
445+
446+
Assert.Equal(0, num.AsDouble());
447+
Assert.Equal(0m, dec.AsDecimal());
448+
}
449+
450+
[Fact]
451+
public void AsBooleanTests()
452+
{
453+
var x = FormulaValue.New(true);
454+
455+
bool b = x.AsBoolean();
456+
Assert.True(x.AsBoolean());
457+
458+
var x2 = FormulaValue.NewBlank(FormulaType.Boolean);
459+
bool b2 = x2.AsBoolean();
460+
Assert.False(x2.AsBoolean());
461+
}
462+
463+
[Fact]
464+
public void AsNumberFailTests()
465+
{
466+
StringValue num = FormulaValue.New("1.2");
467+
468+
Assert.Throws<InvalidOperationException>(() => num.AsDouble());
469+
Assert.Throws<InvalidOperationException>(() => num.AsDecimal());
470+
Assert.Throws<InvalidOperationException>(() => num.AsBoolean());
471+
}
426472
}
427473

428474
public static class FormulaValueExtensions

0 commit comments

Comments
 (0)