diff --git a/source/engine/format.js b/source/engine/format.js index 75f0086c5..76d836622 100644 --- a/source/engine/format.js +++ b/source/engine/format.js @@ -75,7 +75,8 @@ export function formatValue({ numberFormatter({ style: 'decimal', minimumFractionDigits, - maximumFractionDigits + maximumFractionDigits, + language })(value) + (typeof serializedUnit === 'string' ? `\u00A0${serializedUnit}` : '') ) diff --git a/source/engine/format.test.js b/source/engine/format.test.js new file mode 100644 index 000000000..577dfeab9 --- /dev/null +++ b/source/engine/format.test.js @@ -0,0 +1,27 @@ +import { expect } from 'chai' +import { formatCurrency, formatPercentage, formatValue } from './format' + +describe('format engine values', () => { + it('format currencies', () => { + expect(formatCurrency(12, 'fr')).to.equal('12 €') + expect(formatCurrency(1200, 'fr')).to.equal('1 200 €') + expect(formatCurrency(12, 'en')).to.equal('€ 12') + expect(formatCurrency(12.1)).to.equal('€ 12.10') + expect(formatCurrency(12.123)).to.equal('€ 12.12') + }) + + it('format percentages', () => { + expect(formatPercentage(0.1)).to.equal('10%') + expect(formatPercentage(1)).to.equal('100%') + expect(formatPercentage(0.102)).to.equal('10.2%') + }) + + it('format values', () => { + expect(formatValue({ unit: '€', value: 12 })).to.equal('€12') + expect(formatValue({ unit: '€', value: 12.1 })).to.equal('€12.10') + expect(formatValue({ unit: '€', value: 12, language: 'fr' })).to.equal( + '12 €' + ) + expect(formatValue({ value: 1200, language: 'fr' })).to.equal('1 200') + }) +})