GuidesUniver SheetsFeaturesCoreNumber Format

Number Format

Facade APIHas Paid PlanUniver ServerUniver on Node.jsPreset
βœ…--βœ…UniverSheetsCorePreset

The number format plugin provides the formatting function of numbers in cells, including the number of decimal places, thousands separator, currency symbol, etc.

🚨

The DBNum syntax is not supported.

Usage

In the UI interface, you can set the number format of the cell through the menu bar. Or set the s.n.pattern field in the cell data ICellData object.

{
  v: 123456.789,
  s: {
    n: {
      pattern: '#,##0.00'
    }
  }
}

Facade API

Set number format 0.5.2+

univerAPI.getHooks().onRendered(() => {
  const sheet = univerAPI.getActiveWorkbook().getActiveSheet();
 
  // get range
  const range = sheet.getRange(0, 0, 10, 10); // A1:J10
 
  // For specific parameters, please refer to: https://support.microsoft.com/en-us/office/number-format-codes-5026bbd6-04bc-48cd-bf33-80f18b4eae68
  range.setNumberFormat('#,###');
})

FAQ

I don’t want the cell to be converted by default when entering a number starting with 0 or a number that can be converted to a date format

You can set the cell format to text format, or add a single quote ' (force text) before entering the number.

Set s.n.pattern in the ICellData object:

import { DEFAULT_TEXT_FORMAT } from '@univerjs/engine-numfmt';
 
{
  v: '012.0',
  s: {
    n: {
      pattern: DEFAULT_TEXT_FORMAT // Text format
    }
  }
}

Set the cell format through univerAPI:

range.setNumberFormat(DEFAULT_TEXT_FORMAT);

Was this page helpful?