-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #884 from Deltares/868-bounding-box-accuracy
BoundingBox: improved the string conversion to ensure rounding
- Loading branch information
Showing
5 changed files
with
135 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @vitest-environment jsdom | ||
*/ | ||
|
||
import { expect, test, describe } from 'vitest' | ||
import { | ||
floatPrecision, | ||
roundToStepPrecision, | ||
roundToDecimalPlaces, | ||
} from './math' | ||
|
||
describe('floatPrecision', () => { | ||
test('with 4 decimal places', () => { | ||
expect(floatPrecision(54.6545)).toEqual(4) | ||
}) | ||
|
||
test('with 0 decimal places', () => { | ||
expect(floatPrecision(54)).toEqual(0) | ||
}) | ||
|
||
test('with 1 decimal place', () => { | ||
expect(floatPrecision(54.1)).toEqual(1) | ||
}) | ||
|
||
test('with negative number', () => { | ||
expect(floatPrecision(-54.6545)).toEqual(4) | ||
}) | ||
}) | ||
|
||
describe('roundToStepPrecision', () => { | ||
test('round to 0.1 with whole number', () => { | ||
expect(roundToStepPrecision(54, 0.1)).toEqual(54) | ||
}) | ||
|
||
test('round to 0.01 with single decimal number', () => { | ||
expect(roundToStepPrecision(54.1, 0.01)).toEqual(54.1) | ||
}) | ||
|
||
test('round to 0.1', () => { | ||
expect(roundToStepPrecision(54.6545, 0.1)).toEqual(54.7) | ||
}) | ||
|
||
test('round to 0.01', () => { | ||
expect(roundToStepPrecision(54.6545, 0.01)).toEqual(54.65) | ||
}) | ||
|
||
test('round to 1', () => { | ||
expect(roundToStepPrecision(54.6545, 1)).toEqual(55) | ||
}) | ||
|
||
test('round to 10', () => { | ||
expect(roundToStepPrecision(54.6545, 10)).toEqual(55) | ||
}) | ||
|
||
test('round to 100', () => { | ||
expect(roundToStepPrecision(54.6545, 100)).toEqual(55) | ||
}) | ||
|
||
test('round with negative number', () => { | ||
expect(roundToStepPrecision(-54.6545, 0.1)).toEqual(-54.7) | ||
}) | ||
}) | ||
|
||
describe('roundToDecimalPlaces', () => { | ||
test('round to 2 decimal places', () => { | ||
expect(roundToDecimalPlaces(54.6545, 2)).toEqual(54.65) | ||
}) | ||
|
||
test('round to 1 decimal place', () => { | ||
expect(roundToDecimalPlaces(54.6545, 1)).toEqual(54.7) | ||
}) | ||
|
||
test('round to 0 decimal places', () => { | ||
expect(roundToDecimalPlaces(54.6545, 0)).toEqual(55) | ||
}) | ||
|
||
test('round to negative decimal places', () => { | ||
expect(roundToDecimalPlaces(54.6545, -1)).toEqual(NaN) | ||
}) | ||
|
||
test('round with negative number', () => { | ||
expect(roundToDecimalPlaces(-54.6545, 2)).toEqual(-54.65) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Get decimal places of float | ||
* @param {number} a - float number | ||
* @returns {number} The number of decimal places of the float number. | ||
* @example | ||
* floatPrecision(54.6545) // 4 | ||
*/ | ||
export function floatPrecision(a: number): number { | ||
if (!isFinite(a)) return 0 | ||
var e = 1, | ||
p = 0 | ||
while (Math.round(a * e) / e !== a) { | ||
e *= 10 | ||
p++ | ||
} | ||
return p | ||
} | ||
|
||
/** | ||
* Round a number to the nearest step precision | ||
* @param {number} value - The number to round. | ||
* @param {number} step - The step to round to. | ||
* @returns {number} The rounded number. | ||
* @example | ||
* roundToStepPrecision(54.6545, 0.1) // 54.7 | ||
*/ | ||
export function roundToStepPrecision(value: number, step: number): number { | ||
const precision = floatPrecision(step) | ||
return roundToDecimalPlaces(value, precision) | ||
} | ||
|
||
/** | ||
* Round a number to a specified number of decimal places | ||
* @param {number} value - The number to round. | ||
* @param {number} decimalPlaces - The number of decimal places to round to. | ||
* @returns {number} The rounded number. | ||
* @example | ||
* roundToDecimalPlaces(54.6545, 2) // 54.65 | ||
*/ | ||
export function roundToDecimalPlaces( | ||
value: number, | ||
decimalPlaces: number, | ||
): number { | ||
const num = Number(`${value}e+${decimalPlaces}`) | ||
return Number(Math.round(num) + `e-${decimalPlaces}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters