본문으로 건너뛰기Skip to content
예제 첫화면Examples home

숫자 표시 형식 정하기Set the number display format

컬럼의 format.number로 소수 자릿수, 천 단위 구분, 접두·접미, 반올림 방식, 음수 모양, 빈 값 글자를 선언해요. 값은 그대로이고 보이는 글자만 바뀌어요.format.number on a column declares decimals, thousands separator, prefix and suffix, rounding mode, negative style and the text for empty values. The value is unchanged; only the shown text differs.

입문Beginner
직접 써 보기Live preview숫자 표시 형식 정하기Set the number display format

코드Code

탭을 골라 코드를 복사하세요. 위 화면은 이 코드를 그대로 실행한 결과예요.Pick a tab and copy the code. The preview above runs exactly this code.

이 화면은 새 모양 옵션을 켠 모양이에요: density: 'spacious', iconStyle: 'soft', motion: true (코드에 없으면 기본 모양으로 보여요)This preview has the new-look options on: density: 'spacious', iconStyle: 'soft', motion: true (without them in your code you get the default look)

빌드 도구(Vite 등)를 쓰는 프로젝트용 모듈 파일이에요. 설치 없이 바로 돌려 보려면 HTML 탭이나 직접 해보기를 쓰세요.

버튼을 누르면 이 예제의 코드가 StackBlitz나 CodeSandbox로 전송돼요. 누르기 전에는 아무것도 보내지 않아요.

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="round">내림 2자리</button>
    <button type="button" id="reset">처음 형식</button>
  </div>
  <div id="grid" style="height: 230px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('round').addEventListener('click', () => actions.round(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
const rows = [
  { id: 1, item: '노트북', price: 1234567.891, delta: 12.345 },
  { id: 2, item: '마우스', price: 25900, delta: -1234.5 },
  { id: 3, item: '케이블', price: 1.005, delta: -0.004 },
  { id: 4, item: '서비스', price: 0, delta: null },
];
// 컬럼의 format.number 한 곳에 자릿수·천 단위·접두·접미·반올림·음수 모양을 모아 써요.
// 금액은 원 단위 천 단위 구분, 증감은 소수 1자리에 음수는 괄호, 값이 없으면 '-'를 보여요.
export const options = {
  columns: [
    { id: 'id', title: '번호', kind: 'number', width: 70 },
    { id: 'item', title: '품목', width: 110 },
    {
      id: 'price',
      title: '금액',
      kind: 'number',
      width: 150,
      format: { number: { decimals: 0, grouping: true, suffix: '원', rounding: 'round' } },
    },
    {
      id: 'delta',
      title: '증감',
      kind: 'number',
      width: 120,
      format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(_api, log) {
  log({
    ko: '1.005는 소수 자릿수를 줄여도 오차 없이 반올림해요. 음수는 괄호로 보여요.',
    en: '1.005 rounds without float errors. Negative numbers show in parentheses.',
  });
  return () => undefined;
}
export const actions = {
  round: (api) => api.setColumns(options.columns.map((c) => c.id === 'price'
    ? {
      ...c,
      format: { number: { decimals: 2, grouping: true, rounding: 'floor' } },
    }
    : c)),
  reset: (api) => api.setColumns(options.columns),
};

Module files for a project with a bundler such as Vite. To run it with no setup, use the HTML tab or Try it.

When you click, the example code is sent to StackBlitz or CodeSandbox. Nothing is sent before you click.

index.html
<div id="app">
  <div class="toolbar">
    <button type="button" id="round">Floor, 2 decimals</button>
    <button type="button" id="reset">Original format</button>
  </div>
  <div id="grid" style="height: 230px"></div>
</div>
<script type="module" src="./main.js"></script>
main.js
import { createGrid } from 'maxgrid';
import { actions, options, setup } from './example';
const api = createGrid(document.getElementById('grid'), options);
setup(api, console.log);
document.getElementById('round').addEventListener('click', () => actions.round(api));
document.getElementById('reset').addEventListener('click', () => actions.reset(api));
example.js
const rows = [
  { id: 1, item: 'Laptop', price: 1234.568, delta: 12.345 },
  { id: 2, item: 'Mouse', price: 25.9, delta: -1234.5 },
  { id: 3, item: 'Cable', price: 1.005, delta: -0.004 },
  { id: 4, item: 'Service', price: 0, delta: null },
];
// format.number on a column gathers decimals, thousands separator, prefix, suffix, rounding and negative style in one place.
// Amounts show a $ prefix with thousands separators, changes show 1 decimal with negatives in parentheses, and an empty value shows '-'.
export const options = {
  columns: [
    { id: 'id', title: 'No.', kind: 'number', width: 70 },
    { id: 'item', title: 'Item', width: 110 },
    {
      id: 'price',
      title: 'Amount',
      kind: 'number',
      width: 150,
      format: { number: { decimals: 0, grouping: true, prefix: '$', rounding: 'round' } },
    },
    {
      id: 'delta',
      title: 'Change',
      kind: 'number',
      width: 120,
      format: { number: { decimals: 1, negative: 'parens', suffix: '%', emptyText: '-' } },
    },
  ],
  rows,
  rowKey: 'id',
};
export function setup(_api, log) {
  log('1.005 rounds without float errors. Negative numbers show in parentheses.');
  return () => undefined;
}
export const actions = {
  round: (api) => api.setColumns(options.columns.map((c) => c.id === 'price'
    ? {
      ...c,
      format: {
        number: { decimals: 2, grouping: true, prefix: '$', rounding: 'floor' },
      },
    }
    : c)),
  reset: (api) => api.setColumns(options.columns),
};