@brightspace-ui/intl
Advanced tools
Comparing version 3.3.1 to 3.3.2
@@ -46,14 +46,2 @@ import { | ||
function formatExponentialDecimal(value) { | ||
// Get a value should be like "1.25e-8" | ||
const pieces = value.split('e-'); | ||
let ret = pieces[0].replace('.', ''); | ||
const zeroCount = parseInt(pieces[1]) - 1; | ||
for (let i = 0; i < zeroCount; i++) { | ||
ret = '0' + ret; | ||
} | ||
return ret; | ||
} | ||
function validateFormatOptions(options) { | ||
@@ -196,39 +184,21 @@ | ||
const isNegative = value < 0; | ||
value = Math.abs(value); | ||
const precisionScaling = Math.pow(10, options.maximumFractionDigits); | ||
// round to desired precision | ||
value = Math.abs(Math.round(value * precisionScaling) / precisionScaling); | ||
const strValue = new Intl.NumberFormat( | ||
'en-US', | ||
{ | ||
signDisplay: 'never', | ||
maximumFractionDigits: options.maximumFractionDigits, | ||
minimumFractionDigits: options.minimumFractionDigits, | ||
useGrouping: false | ||
} | ||
).format(value); | ||
const integerValue = Math.floor(value); | ||
let ret = formatPositiveInteger(parseInt(strValue), descriptor); | ||
let ret = formatPositiveInteger(integerValue, descriptor); | ||
const hasDecimal = value !== integerValue; | ||
let decimalStr = null; | ||
if (hasDecimal) { | ||
let decimalValue = (value * precisionScaling - integerValue * precisionScaling) / precisionScaling; | ||
decimalValue = Math.round(decimalValue * precisionScaling) / precisionScaling; | ||
// get a string of 0.xxx, or exponent of x.xe-x | ||
decimalStr = '' + decimalValue; | ||
if (decimalValue.toExponential() === decimalStr) { | ||
// Get a string with leading zeros | ||
decimalStr = formatExponentialDecimal(decimalStr); | ||
} else { | ||
// the first decimal place is index 2 | ||
decimalStr = decimalStr.slice(2); | ||
} | ||
} else if (options.minimumFractionDigits > 0) { | ||
decimalStr = ''; | ||
const decimalIndex = strValue.indexOf('.'); | ||
if (decimalIndex > -1) { | ||
ret += descriptor.symbols.decimal + strValue.substr(decimalIndex + 1); | ||
} | ||
if (decimalStr !== null) { | ||
while (decimalStr.length < options.minimumFractionDigits) { | ||
decimalStr += '0'; | ||
} | ||
ret += descriptor.symbols.decimal + decimalStr; | ||
} | ||
const pattern = isNegative | ||
@@ -235,0 +205,0 @@ ? descriptor.patterns.decimal.negativePattern |
{ | ||
"name": "@brightspace-ui/intl", | ||
"version": "3.3.1", | ||
"version": "3.3.2", | ||
"description": "Internationalization APIs for number, date, time and file size formatting and parsing in D2L Brightspace.", | ||
@@ -5,0 +5,0 @@ "main": "lib/number.js", |
68605
1535