Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format NumeralJS percentages without multiplying by 100

In Handsontable, you can use the format option on a numeric cell to format the values accordingly. Since it uses NumeralJS, I looked through the documentation to see how to format a number to just add the % sign and not multiply by 100 but can't find how to do this without setting my own.

Example cell:

{
    "type": "numeric",
    "format": "0.00%"
}

When the value is 7, it displays "700.00%". I'd like it to show `"7.00%". Any way of doing this in handsontable?

like image 995
ZekeDroid Avatar asked Dec 31 '14 17:12

ZekeDroid


People also ask

How do I format a percentage in Excel without multiplying by 100?

If you want to add percentage sign to a number without multiplying the number by 100, such as to change 6 into 6% in Excel with Percentage Style, you should have the general number divided by 100 first, and then apply the Percentage Style to the numbers for displaying them as percentage 6% in Excel.

How do I create a custom percentage format in Excel?

On the Home tab, in the Number group, click the icon next to Number to display the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Percentage. In the Decimal places box, enter the number of decimal places that you want to display.

How do I import numerals?

You can run npm install numeral and then import the numeral package in whatever component you want to use it in. npm install numeralimport numeral from 'numeral'; Now that you're referencing the library, we can start using it!

What is numeral js?

js. A javascript library for formatting and manipulating numbers. Website and documentation.


2 Answers

For anyone still looking for the easiest way of achieving this, there is a numeral option you can set like so:

numeral.options.scalePercentBy100 = false;

Credit to Csaba Toth for creating the PR. PR Here

like image 92
Chris Avatar answered Jan 02 '23 12:01

Chris


I do not think there is a way to do this currently without making changes to the numeral.js file.

If you want, you can edit function formatPercentage (found in numberal.js) by removing the * 100 part:

function formatPercentage (n, format, roundingFunction) {
    var space = '',
        output,
        value = n._value/* * 100*/; // comment out or remove * 100 here

    ...
}
like image 44
halafi Avatar answered Jan 02 '23 12:01

halafi