Skip to content

Commit d9ba241

Browse files
ScrumMoOx
authored andcommitted
wrong rounding. (MoOx#10)
* Not correct rounding. * fixed lint error report * added message to my test. * syntax fix * Fixed wrong round
1 parent b924f5d commit d9ba241

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function reduceCSSCalc(value, decimalPrecision) {
6161

6262
if (unit === "%") {
6363
// Convert percentages to numbers, to handle expressions like: 50% * 50% (will become: 25%):
64+
// console.log(expression)
6465
expression = expression.replace(/\b[0-9\.]+%/g, function(percent) {
6566
return parseFloat(percent.slice(0, -1)) * 0.01
6667
})
@@ -84,7 +85,9 @@ function reduceCSSCalc(value, decimalPrecision) {
8485

8586
// adjust rounding shit
8687
// (0.1 * 0.2 === 0.020000000000000004)
87-
result = Math.round(result * decimalPrecision) / decimalPrecision
88+
if (functionIdentifier.length || unit === "%") {
89+
result = Math.round(result * decimalPrecision) / decimalPrecision
90+
}
8891

8992
// We don't need units for zero values...
9093
if (result !== 0) {

test/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ test("reduce complexe css calc()", function(t) {
5555
t.equal(reduceCSSCalc("calc(((1rem * 0.75) * 1.5) - 1px)"), "calc(1.125rem - 1px)", "multiple units with implicit calc")
5656
t.equal(reduceCSSCalc("calc(-1px + (1.5 * (1rem * 0.75)))"), "calc(-1px + 1.125rem)", "multiple units with implicit calc, reverse order")
5757
t.equal(reduceCSSCalc("calc(2rem * (2 * (2 + 3)) + 4 + (5/2))"), "26.5rem", "complex math formula works correctly")
58-
5958
t.equal(reduceCSSCalc("calc((4 * 2) + 4.2 + 1 + (2rem * .4) + (2px * .4))"), "calc(8 + 4.2 + 1 + 0.8rem + 0.8px)", "handle long formula")
59+
t.equal(reduceCSSCalc("calc((2 * 100) / 12)"), reduceCSSCalc("calc((100 / 12) * 2)"), "indentical, wrong rounded")
60+
t.equal(reduceCSSCalc("calc((2 * 100) / 12)", 3), "16.667", "indentical rounded with options")
61+
t.equal(reduceCSSCalc("calc((100 / 12) * 2)", 3), "16.667", "indentical rounded with options")
6062
t.end()
6163
})
6264

0 commit comments

Comments
 (0)