Skip to content

Commit 89c7bce

Browse files
committed
improve number formatting
1 parent a770be0 commit 89c7bce

File tree

6 files changed

+124
-119
lines changed

6 files changed

+124
-119
lines changed

src/main/java/com/gargoylesoftware/css/parser/LexicalUnitImpl.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -736,17 +736,22 @@ private void appendParams(final StringBuilder sb) {
736736
}
737737

738738
private String getTrimedDoubleValue() {
739-
final double f = getDoubleValue();
740-
final int i = (int) f;
739+
final double d = getDoubleValue();
740+
final int i = (int) d;
741741

742-
if (f - i == 0) {
743-
return Integer.toString((int) f);
742+
if (d - i == 0) {
743+
return Integer.toString(i);
744744
}
745745

746-
final DecimalFormat decimalFormat = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
747-
decimalFormat.setGroupingUsed(false);
748-
decimalFormat.setMaximumFractionDigits(4);
749-
return decimalFormat.format(f);
746+
// i know this is uggly - suggestions are welcome
747+
final String str = Double.toString(d);
748+
if (str.contains("E")) {
749+
final DecimalFormat decimalFormat = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
750+
decimalFormat.setGroupingUsed(false);
751+
decimalFormat.setMaximumFractionDigits(7);
752+
return decimalFormat.format(d);
753+
}
754+
return str;
750755
}
751756

752757
/**

src/test/java/com/gargoylesoftware/css/dom/CSSValueImplTest.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void attr() throws Exception {
4949
*/
5050
@Test
5151
public void cm() throws Exception {
52-
final LexicalUnit lu = LexicalUnitImpl.createCentimeter(null, 1.2f);
52+
final LexicalUnit lu = LexicalUnitImpl.createCentimeter(null, 1.2);
5353
final CSSValueImpl value = new CSSValueImpl(lu, false);
5454

5555
Assert.assertEquals("1.2cm", value.getCssText());
@@ -91,7 +91,7 @@ public void counter() throws Exception {
9191
*/
9292
@Test
9393
public void deg() throws Exception {
94-
final LexicalUnit lu = LexicalUnitImpl.createDegree(null, 1.2f);
94+
final LexicalUnit lu = LexicalUnitImpl.createDegree(null, 1.2);
9595
final CSSValueImpl value = new CSSValueImpl(lu, false);
9696

9797
Assert.assertEquals("1.2deg", value.getCssText());
@@ -112,7 +112,7 @@ public void deg() throws Exception {
112112
*/
113113
@Test
114114
public void dimension() throws Exception {
115-
final LexicalUnit lu = LexicalUnitImpl.createDimension(null, 1.2f, "lumen");
115+
final LexicalUnit lu = LexicalUnitImpl.createDimension(null, 1.2, "lumen");
116116
final CSSValueImpl value = new CSSValueImpl(lu, false);
117117

118118
Assert.assertEquals("1.2lumen", value.getCssText());
@@ -133,7 +133,7 @@ public void dimension() throws Exception {
133133
*/
134134
@Test
135135
public void ems() throws Exception {
136-
final LexicalUnit lu = LexicalUnitImpl.createEm(null, 1.2f);
136+
final LexicalUnit lu = LexicalUnitImpl.createEm(null, 1.2);
137137
final CSSValueImpl value = new CSSValueImpl(lu, false);
138138

139139
Assert.assertEquals("1.2em", value.getCssText());
@@ -154,7 +154,7 @@ public void ems() throws Exception {
154154
*/
155155
@Test
156156
public void emx() throws Exception {
157-
final LexicalUnit lu = LexicalUnitImpl.createEx(null, 1.2f);
157+
final LexicalUnit lu = LexicalUnitImpl.createEx(null, 1.2);
158158
final CSSValueImpl value = new CSSValueImpl(lu, false);
159159

160160
Assert.assertEquals("1.2ex", value.getCssText());
@@ -207,7 +207,7 @@ public void functionTwoParams() throws Exception {
207207
*/
208208
@Test
209209
public void gradian() throws Exception {
210-
final LexicalUnit lu = LexicalUnitImpl.createGradian(null, 1.2f);
210+
final LexicalUnit lu = LexicalUnitImpl.createGradian(null, 1.2);
211211
final CSSValueImpl value = new CSSValueImpl(lu, false);
212212

213213
Assert.assertEquals("1.2grad", value.getCssText());
@@ -228,7 +228,7 @@ public void gradian() throws Exception {
228228
*/
229229
@Test
230230
public void hertz() throws Exception {
231-
final LexicalUnit lu = LexicalUnitImpl.createHertz(null, 1.2f);
231+
final LexicalUnit lu = LexicalUnitImpl.createHertz(null, 1.2);
232232
final CSSValueImpl value = new CSSValueImpl(lu, false);
233233

234234
Assert.assertEquals("1.2Hz", value.getCssText());
@@ -264,7 +264,7 @@ public void ident() throws Exception {
264264
*/
265265
@Test
266266
public void inch() throws Exception {
267-
final LexicalUnit lu = LexicalUnitImpl.createInch(null, 1.2f);
267+
final LexicalUnit lu = LexicalUnitImpl.createInch(null, 1.2);
268268
final CSSValueImpl value = new CSSValueImpl(lu, false);
269269

270270
Assert.assertEquals("1.2in", value.getCssText());
@@ -285,7 +285,7 @@ public void inch() throws Exception {
285285
*/
286286
@Test
287287
public void kiloHertz() throws Exception {
288-
final LexicalUnit lu = LexicalUnitImpl.createKiloHertz(null, 1.2f);
288+
final LexicalUnit lu = LexicalUnitImpl.createKiloHertz(null, 1.2);
289289
final CSSValueImpl value = new CSSValueImpl(lu, false);
290290

291291
Assert.assertEquals("1.2kHz", value.getCssText());
@@ -306,7 +306,7 @@ public void kiloHertz() throws Exception {
306306
*/
307307
@Test
308308
public void millimeter() throws Exception {
309-
final LexicalUnit lu = LexicalUnitImpl.createMillimeter(null, 1.2f);
309+
final LexicalUnit lu = LexicalUnitImpl.createMillimeter(null, 1.2);
310310
final CSSValueImpl value = new CSSValueImpl(lu, false);
311311

312312
Assert.assertEquals("1.2mm", value.getCssText());
@@ -327,7 +327,7 @@ public void millimeter() throws Exception {
327327
*/
328328
@Test
329329
public void millisecond() throws Exception {
330-
final LexicalUnit lu = LexicalUnitImpl.createMillisecond(null, 1.2f);
330+
final LexicalUnit lu = LexicalUnitImpl.createMillisecond(null, 1.2);
331331
final CSSValueImpl value = new CSSValueImpl(lu, false);
332332

333333
Assert.assertEquals("1.2ms", value.getCssText());
@@ -348,7 +348,7 @@ public void millisecond() throws Exception {
348348
*/
349349
@Test
350350
public void numberDouble() throws Exception {
351-
final LexicalUnit lu = LexicalUnitImpl.createNumber(null, 1.2f);
351+
final LexicalUnit lu = LexicalUnitImpl.createNumber(null, 1.2);
352352
final CSSValueImpl value = new CSSValueImpl(lu, false);
353353

354354
Assert.assertEquals("1.2", value.getCssText());
@@ -390,7 +390,7 @@ public void numberInt() throws Exception {
390390
*/
391391
@Test
392392
public void pica() throws Exception {
393-
final LexicalUnit lu = LexicalUnitImpl.createPica(null, 1.2f);
393+
final LexicalUnit lu = LexicalUnitImpl.createPica(null, 1.2);
394394
final CSSValueImpl value = new CSSValueImpl(lu, false);
395395

396396
Assert.assertEquals("1.2pc", value.getCssText());
@@ -411,7 +411,7 @@ public void pica() throws Exception {
411411
*/
412412
@Test
413413
public void percentage() throws Exception {
414-
final LexicalUnit lu = LexicalUnitImpl.createPercentage(null, 1.2f);
414+
final LexicalUnit lu = LexicalUnitImpl.createPercentage(null, 1.2);
415415
final CSSValueImpl value = new CSSValueImpl(lu, false);
416416

417417
Assert.assertEquals("1.2%", value.getCssText());
@@ -432,7 +432,7 @@ public void percentage() throws Exception {
432432
*/
433433
@Test
434434
public void point() throws Exception {
435-
final LexicalUnit lu = LexicalUnitImpl.createPoint(null, 1.2f);
435+
final LexicalUnit lu = LexicalUnitImpl.createPoint(null, 1.2);
436436
final CSSValueImpl value = new CSSValueImpl(lu, false);
437437

438438
Assert.assertEquals("1.2pt", value.getCssText());
@@ -453,7 +453,7 @@ public void point() throws Exception {
453453
*/
454454
@Test
455455
public void pixel() throws Exception {
456-
final LexicalUnit lu = LexicalUnitImpl.createPixel(null, 1.2f);
456+
final LexicalUnit lu = LexicalUnitImpl.createPixel(null, 1.2);
457457
final CSSValueImpl value = new CSSValueImpl(lu, false);
458458

459459
Assert.assertEquals("1.2px", value.getCssText());
@@ -474,7 +474,7 @@ public void pixel() throws Exception {
474474
*/
475475
@Test
476476
public void radian() throws Exception {
477-
final LexicalUnit lu = LexicalUnitImpl.createRadian(null, 1.2f);
477+
final LexicalUnit lu = LexicalUnitImpl.createRadian(null, 1.2);
478478
final CSSValueImpl value = new CSSValueImpl(lu, false);
479479

480480
Assert.assertEquals("1.2rad", value.getCssText());
@@ -530,7 +530,7 @@ public void rect() throws Exception {
530530
*/
531531
@Test
532532
public void rem() throws Exception {
533-
final LexicalUnit lu = LexicalUnitImpl.createRem(null, 1.2f);
533+
final LexicalUnit lu = LexicalUnitImpl.createRem(null, 1.2);
534534
final CSSValueImpl value = new CSSValueImpl(lu, false);
535535

536536
Assert.assertEquals("1.2rem", value.getCssText());
@@ -551,7 +551,7 @@ public void rem() throws Exception {
551551
*/
552552
@Test
553553
public void ch() throws Exception {
554-
final LexicalUnit lu = LexicalUnitImpl.createCh(null, 1.2f);
554+
final LexicalUnit lu = LexicalUnitImpl.createCh(null, 1.2);
555555
final CSSValueImpl value = new CSSValueImpl(lu, false);
556556

557557
Assert.assertEquals("1.2ch", value.getCssText());
@@ -572,7 +572,7 @@ public void ch() throws Exception {
572572
*/
573573
@Test
574574
public void vw() throws Exception {
575-
final LexicalUnit lu = LexicalUnitImpl.createVw(null, 1.2f);
575+
final LexicalUnit lu = LexicalUnitImpl.createVw(null, 1.2);
576576
final CSSValueImpl value = new CSSValueImpl(lu, false);
577577

578578
Assert.assertEquals("1.2vw", value.getCssText());
@@ -593,7 +593,7 @@ public void vw() throws Exception {
593593
*/
594594
@Test
595595
public void vh() throws Exception {
596-
final LexicalUnit lu = LexicalUnitImpl.createVh(null, 1.2f);
596+
final LexicalUnit lu = LexicalUnitImpl.createVh(null, 1.2);
597597
final CSSValueImpl value = new CSSValueImpl(lu, false);
598598

599599
Assert.assertEquals("1.2vh", value.getCssText());
@@ -614,7 +614,7 @@ public void vh() throws Exception {
614614
*/
615615
@Test
616616
public void vmin() throws Exception {
617-
final LexicalUnit lu = LexicalUnitImpl.createVMin(null, 1.2f);
617+
final LexicalUnit lu = LexicalUnitImpl.createVMin(null, 1.2);
618618
final CSSValueImpl value = new CSSValueImpl(lu, false);
619619

620620
Assert.assertEquals("1.2vmin", value.getCssText());
@@ -635,7 +635,7 @@ public void vmin() throws Exception {
635635
*/
636636
@Test
637637
public void vmax() throws Exception {
638-
final LexicalUnit lu = LexicalUnitImpl.createVMax(null, 1.2f);
638+
final LexicalUnit lu = LexicalUnitImpl.createVMax(null, 1.2);
639639
final CSSValueImpl value = new CSSValueImpl(lu, false);
640640

641641
Assert.assertEquals("1.2vmax", value.getCssText());
@@ -689,7 +689,7 @@ public void rgbColor() throws Exception {
689689
*/
690690
@Test
691691
public void second() throws Exception {
692-
final LexicalUnit lu = LexicalUnitImpl.createSecond(null, 1.2f);
692+
final LexicalUnit lu = LexicalUnitImpl.createSecond(null, 1.2);
693693
final CSSValueImpl value = new CSSValueImpl(lu, false);
694694

695695
Assert.assertEquals("1.2s", value.getCssText());

src/test/java/com/gargoylesoftware/css/dom/PropteryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void defaultConstructor() throws Exception {
5151
Assert.assertEquals("\"MyValue\"", prop.getValue().toString());
5252
Assert.assertFalse(prop.isImportant());
5353

54-
lu = LexicalUnitImpl.createPixel(null, 11f);
54+
lu = LexicalUnitImpl.createPixel(null, 11);
5555
prop.setValue(new CSSValueImpl(lu, true));
5656
Assert.assertEquals("MyName: 11px", prop.toString());
5757
Assert.assertEquals("MyName", prop.getName());
@@ -84,7 +84,7 @@ public void defaultConstructorNoValueImportant() throws Exception {
8484
*/
8585
@Test
8686
public void constructWithParams() throws Exception {
87-
final LexicalUnit lu = LexicalUnitImpl.createCentimeter(null, 13.2f);
87+
final LexicalUnit lu = LexicalUnitImpl.createCentimeter(null, 13.2);
8888
final Property prop = new Property("MyName", new CSSValueImpl(lu, true), false);
8989
Assert.assertEquals("MyName: 13.2cm", prop.toString());
9090
Assert.assertEquals("MyName", prop.getName());

src/test/java/com/gargoylesoftware/css/parser/CSS3ParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ public void varExpressions() throws Exception {
13721372

13731373
// digits are trimmed to 4
13741374
expression("h1 { margin-right: calc(-66.66667% * var(--space-x-reverse)) }",
1375-
"h1 { margin-right: calc(-66.6667% * var(--space-x-reverse)); }");
1375+
"h1 { margin-right: calc(-66.66667% * var(--space-x-reverse)); }");
13761376

13771377
// empty fallback values
13781378
expression("h1 { top: var(--tailwind-empty, ) }",

0 commit comments

Comments
 (0)