1.0.6 • Published 4 years ago

cardinal-convert v1.0.6

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

Introduce

Convert numbers to cardinal numbers, or amount description

API

covert

paramsdescriptiontypedefault
numnumber to be convertednumber
formatAmountconvert value to amount formatnumber | Functionfalse

locale

paramsdescriptiontypedefault
localeconvert lanugagezh | es | enzh

Usage

import convert, { locale } from 'cardinal-convert';

// zh

// 转成大写
console.log(convert(90009)) // 玖万零玖
// 格式化金额
console.log(convert(90009, true)) // 玖万零玖元整
console.log(convert(90009.9, true)) // 玖万零玖元玖角

// es

locale('es')
//  convert cardinal number by default, ignore points
console.log(convert(199.02)) // ciento noventa y nueve
// convert peso currency by default
console.log(convert(199.02, true)) // ciento noventa y nueve pesos 02/100 MIN


// en

locale('en')
//  convert cardinal number by default
console.log(convert(73)) // seventy-three
// convert amount format
console.log(convert(73.02, true)) // seventy-three and cents two only

// or custom  convert points function
const formatPoints = (num: number): string[] => {
  const points = getPoints(num)
  let pointsList = getLessThanHundred(points)
  if (num !== 0) {
    return ['and point', ...pointsList, 'only']
  }
  return ['only']
}

console.log(convert(73.02, formatPoints)) // seventy-three and point two only

Support language

  • Chinese
  • Spanish
  • English

Test

File% Stmts% Branch% Funcs% LinesUncovered Line #s
en.ts10096.8810010061
es.ts10093.7510010061,77
zh.ts10096.8810010045