Skip to content

Commit e41759f

Browse files
feat(PercentFormatter): Implement FormatDouble and ParseDouble
1 parent 3e103af commit e41759f

File tree

6 files changed

+619
-316
lines changed

6 files changed

+619
-316
lines changed

src/Uno.UI.Tests/Windows_Globalization/Given_DecimalFormatter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ SignificantDigits 0 int
176176

177177
[DataTestMethod]
178178
[DataRow("1.2", 1.2)]
179+
[DataRow("1.2 ", null)]
180+
[DataRow(" 1.2", null)]
179181
[DataRow("1.20", 1.2)]
180182
[DataRow("12,34.2", null)]
181183
[DataRow("0", 0d)]
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using Windows.Globalization.NumberFormatting;
4+
5+
namespace Uno.UI.Tests.Windows_Globalization
6+
{
7+
[TestClass]
8+
public class Given_PercentFormatter
9+
{
10+
[DataTestMethod]
11+
[DataRow(double.PositiveInfinity, "∞")]
12+
[DataRow(double.NegativeInfinity, "-∞")]
13+
[DataRow(double.NaN, "NaN")]
14+
public void When_FormatSpecialDouble(double value, string expected)
15+
{
16+
var formatter = new PercentFormatter();
17+
var actual = formatter.FormatDouble(value);
18+
19+
Assert.AreEqual(expected, actual);
20+
}
21+
22+
[DataTestMethod]
23+
[DataRow(0.015d, 1, 2, "1.50%")]
24+
[DataRow(0.01567d, 1, 2, "1.567%")]
25+
[DataRow(0.015602d, 1, 2, "1.5602%")]
26+
[DataRow(0d, 0, 0, "0%")]
27+
[DataRow(-0d, 0, 0, "0%")]
28+
[DataRow(0d, 0, 2, ".00%")]
29+
[DataRow(-0d, 0, 2, ".00%")]
30+
[DataRow(0d, 2, 0, "00%")]
31+
[DataRow(-0d, 2, 0, "00%")]
32+
[DataRow(0d, 3, 1, "000.0%")]
33+
[DataRow(-0d, 3, 1, "000.0%")]
34+
public void When_FormatDouble(double value, int integerDigits, int fractionDigits, string expected)
35+
{
36+
var formatter = new PercentFormatter();
37+
formatter.IntegerDigits = integerDigits;
38+
formatter.FractionDigits = fractionDigits;
39+
40+
var formatted = formatter.FormatDouble(value);
41+
Assert.AreEqual(expected, formatted);
42+
}
43+
44+
[DataTestMethod]
45+
[DataRow(12.34, 2, 0, "1,234%")]
46+
[DataRow(12.34, 6, 0, "001,234%")]
47+
[DataRow(12.3456, 2, 2, "1,234.56%")]
48+
[DataRow(12.340, 6, 2, "001,234.00%")]
49+
[DataRow(12.340, 6, 0, "001,234%")]
50+
public void When_FormatDoubleWithIsGroupSetTrue(double value, int integerDigits, int fractionDigits, string expected)
51+
{
52+
var formatter = new PercentFormatter();
53+
formatter.IntegerDigits = integerDigits;
54+
formatter.FractionDigits = fractionDigits;
55+
formatter.IsGrouped = true;
56+
57+
var formatted = formatter.FormatDouble(value);
58+
Assert.AreEqual(expected, formatted);
59+
}
60+
61+
[DataTestMethod]
62+
[DataRow(0, 0, "-0%")]
63+
[DataRow(0, 2, "-.00%")]
64+
[DataRow(2, 0, "-00%")]
65+
[DataRow(3, 1, "-000.0%")]
66+
public void When_FormatDoubleMinusZeroWithIsZeroSignedSetTrue(int integerDigits, int fractionDigits, string expected)
67+
{
68+
var formatter = new PercentFormatter();
69+
formatter.IntegerDigits = integerDigits;
70+
formatter.FractionDigits = fractionDigits;
71+
formatter.IsZeroSigned = true;
72+
73+
var formatted = formatter.FormatDouble(-0d);
74+
Assert.AreEqual(expected, formatted);
75+
}
76+
77+
[DataTestMethod]
78+
[DataRow(0, 0, "0%")]
79+
[DataRow(0, 2, ".00%")]
80+
[DataRow(2, 0, "00%")]
81+
[DataRow(3, 1, "000.0%")]
82+
public void When_FormatDoubleZeroWithIsZeroSignedSetTrue(int integerDigits, int fractionDigits, string expected)
83+
{
84+
var formatter = new PercentFormatter();
85+
formatter.IntegerDigits = integerDigits;
86+
formatter.FractionDigits = fractionDigits;
87+
formatter.IsZeroSigned = true;
88+
89+
var formatted = formatter.FormatDouble(0d);
90+
Assert.AreEqual(expected, formatted);
91+
}
92+
93+
[DataTestMethod]
94+
[DataRow(0.01d, "1.%")]
95+
public void When_FormatDoubleWithIsDecimalPointerAlwaysDisplayedSetTrue(double value, string expected)
96+
{
97+
var formatter = new PercentFormatter();
98+
formatter.IsDecimalPointAlwaysDisplayed = true;
99+
formatter.FractionDigits = 0;
100+
formatter.IntegerDigits = 0;
101+
102+
var formatted = formatter.FormatDouble(value);
103+
Assert.AreEqual(expected, formatted);
104+
}
105+
106+
[DataTestMethod]
107+
[DataRow(1.234567d, 5, 1, 2, "123.4567%")]
108+
[DataRow(1.234567d, 10, 1, 2, "123.4567000%")]
109+
[DataRow(1.234567d, 2, 1, 2, "123.4567%")]
110+
[DataRow(0.123d, 4, 1, 2, "12.30%")]
111+
[DataRow(0.123d, 4, 1, 0, "12.30%")]
112+
public void When_FormatDoubleWithSpecificSignificantDigits(double value, int significantDigits, int integerDigits, int fractionDigits, string expected)
113+
{
114+
var formatter = new PercentFormatter();
115+
formatter.SignificantDigits = significantDigits;
116+
formatter.IntegerDigits = integerDigits;
117+
formatter.FractionDigits = fractionDigits;
118+
119+
var formatted = formatter.FormatDouble(value);
120+
Assert.AreEqual(expected, formatted);
121+
}
122+
123+
[TestMethod]
124+
public void When_FormatDoubleUsingIncrementNumberRounder()
125+
{
126+
var formatter = new PercentFormatter();
127+
IncrementNumberRounder rounder = new IncrementNumberRounder();
128+
rounder.Increment = 0.5;
129+
formatter.NumberRounder = rounder;
130+
var formatted = formatter.FormatDouble(1.8);
131+
132+
Assert.AreEqual("200.00%", formatted);
133+
}
134+
135+
[TestMethod]
136+
public void When_FormatDoubleUsingSignificantDigitsNumberRounder()
137+
{
138+
var formatter = new PercentFormatter();
139+
SignificantDigitsNumberRounder rounder = new SignificantDigitsNumberRounder();
140+
rounder.SignificantDigits = 1;
141+
formatter.NumberRounder = rounder;
142+
var formatted = formatter.FormatDouble(1.8);
143+
144+
Assert.AreEqual("200.00%", formatted);
145+
}
146+
147+
[TestMethod]
148+
public void When_Initialize()
149+
{
150+
var formatter = new PercentFormatter();
151+
152+
Assert.AreEqual(0, formatter.SignificantDigits);
153+
Assert.AreEqual(1, formatter.IntegerDigits);
154+
Assert.AreEqual(2, formatter.FractionDigits);
155+
Assert.AreEqual(false, formatter.IsGrouped);
156+
Assert.AreEqual(false, formatter.IsZeroSigned);
157+
Assert.AreEqual(false, formatter.IsDecimalPointAlwaysDisplayed);
158+
Assert.AreEqual("en-US", formatter.ResolvedLanguage);
159+
Assert.IsNull(formatter.NumberRounder);
160+
/*
161+
FractionDigits 2 int
162+
GeographicRegion "US" string
163+
IntegerDigits 1 int
164+
IsDecimalPointAlwaysDisplayed false bool
165+
IsGrouped false bool
166+
IsZeroSigned false bool
167+
NumberRounder null WindoGlobalization.NumberFormatting.INumberRounder
168+
NumeralSystem "Latn" string
169+
ResolvedGeographicRegion "ZZ" string
170+
ResolvedLanguage "en-US" string
171+
SignificantDigits 0 int
172+
173+
*/
174+
}
175+
176+
[DataTestMethod]
177+
[DataRow("1.2%", 0.012)]
178+
[DataRow(" 1.2%", null)]
179+
[DataRow("1.2% ", null)]
180+
[DataRow("1.20%", 0.012)]
181+
[DataRow("1.2", null)]
182+
[DataRow("%1.20", null)]
183+
[DataRow("1.20 %", null)]
184+
[DataRow("12,34.2%", null)]
185+
[DataRow("0%", 0d)]
186+
public void When_ParseDouble(string value, double? expected)
187+
{
188+
var formatter = new PercentFormatter();
189+
formatter.FractionDigits = 2;
190+
191+
var actual = formatter.ParseDouble(value);
192+
Assert.AreEqual(expected, actual);
193+
}
194+
195+
[DataTestMethod]
196+
[DataRow("1234.2%", 12.342)]
197+
[DataRow("1,234.2%", 12.342)]
198+
[DataRow("12,34.2%", null)]
199+
public void When_ParseDoubleAndIsGroupSetTrue(string value, double? expected)
200+
{
201+
var formatter = new PercentFormatter();
202+
formatter.FractionDigits = 2;
203+
formatter.IsGrouped = true;
204+
205+
var actual = formatter.ParseDouble(value);
206+
Assert.AreEqual(expected, actual);
207+
}
208+
209+
[DataTestMethod]
210+
[DataRow("1%", 0.01)]
211+
[DataRow("1.%", 0.01)]
212+
public void When_ParseDoubleAndIsDecimalPointAlwaysDisplayedSetTrue(string value, double? expected)
213+
{
214+
var formatter = new PercentFormatter();
215+
formatter.FractionDigits = 2;
216+
formatter.IsDecimalPointAlwaysDisplayed = true;
217+
218+
var actual = formatter.ParseDouble(value);
219+
Assert.AreEqual(expected, actual);
220+
}
221+
222+
[TestMethod]
223+
public void When_ParseDoubleMinusZero()
224+
{
225+
var formatter = new PercentFormatter();
226+
var actual = formatter.ParseDouble("-0%");
227+
bool isNegative = false;
228+
229+
if (actual.HasValue)
230+
{
231+
isNegative = BitConverter.DoubleToInt64Bits(actual.Value) < 0;
232+
}
233+
234+
Assert.AreEqual(true, isNegative);
235+
}
236+
237+
[DataTestMethod]
238+
[DataRow("Arab")]
239+
[DataRow("ArabExt")]
240+
[DataRow("Bali")]
241+
[DataRow("Beng")]
242+
[DataRow("Cham")]
243+
[DataRow("Deva")]
244+
[DataRow("FullWide")]
245+
[DataRow("Gujr")]
246+
[DataRow("Guru")]
247+
[DataRow("Java")]
248+
[DataRow("Kali")]
249+
[DataRow("Khmr")]
250+
[DataRow("Knda")]
251+
[DataRow("Lana")]
252+
[DataRow("LanaTham")]
253+
[DataRow("Laoo")]
254+
[DataRow("Latn")]
255+
[DataRow("Lepc")]
256+
[DataRow("Limb")]
257+
[DataRow("Mlym")]
258+
[DataRow("Mong")]
259+
[DataRow("Mtei")]
260+
[DataRow("Mymr")]
261+
[DataRow("MymrShan")]
262+
[DataRow("Nkoo")]
263+
[DataRow("Olck")]
264+
[DataRow("Orya")]
265+
[DataRow("Saur")]
266+
[DataRow("Sund")]
267+
[DataRow("Talu")]
268+
[DataRow("TamlDec")]
269+
[DataRow("Telu")]
270+
[DataRow("Thai")]
271+
[DataRow("Tibt")]
272+
[DataRow("Vaii")]
273+
public void When_ParseDoubleUsingSpeceficNumeralSystem(string numeralSystem)
274+
{
275+
var formatter = new PercentFormatter();
276+
formatter.NumeralSystem = numeralSystem;
277+
278+
var translator = new NumeralSystemTranslator { NumeralSystem = numeralSystem };
279+
var translated = translator.TranslateNumerals("123456.789%");
280+
281+
var actual = formatter.ParseDouble(translated);
282+
Assert.AreEqual(1234.56789, actual);
283+
}
284+
285+
[TestMethod]
286+
public void When_ParseNotValidDouble()
287+
{
288+
var formatter = new PercentFormatter();
289+
290+
var actual = formatter.ParseDouble("a12%");
291+
Assert.AreEqual(null, actual);
292+
}
293+
}
294+
}

0 commit comments

Comments
 (0)