
세자리 마다 Comma 찍어서 리턴하기
- 숫자 일때, 문자열로 변경하여 리턴
- 세자리 마다 , 찍기
export const commafy = (num : number) : string => {
const str : string[] = num.toString().split('.');
if (str[0].length >=4) {
str[0] = str[0].replace(/(\d)(?=(\d{3})+$)/g, '$1,');
}
return str.join(',');
};
사용 방법
import React from 'react'
import { commafy } from 'src/utils/utility';
function CommafyTest () : React.ReactElement {
const num1 = 1000000;
const num2 = 5000000000;
const num3 = 100;
return (
<div>
<p>{commafy(num1)}</p>
<p>{commafy(num2)}</p>
<p>{commafy(num3)}</p>
</div>
);
}
export default CommafyTest;
잘 봤습니다. 좋은 글 감사합니다.