Code Monkey home page Code Monkey logo

number-to-chinese-words's Introduction

NPM

number-to-chinese-words

Convert a number to chinese words. 由數字轉為中文數目

此項目的測試以萬進制為基準,即10⁸為億,10¹²為兆 https://zh.wikipedia.org/wiki/%E5%84%84

Install

npm install number-to-chinese-words

API

The API is inspired from http://github.com/marlun78/number-to-words This package allow user to use same api from number-to-words for Chinese support.

toOrdinal(number)

將整數加上前置的「第」字。 如果輸入的數字包含小數點,小數點後的數目將會被移除。

var converter = require('number-to-chinese-words');
converter.toOrdinal(21); // => “第21”

toWords(number)

將數目轉換成文字。

// 整數:
var converter = require('number-to-chinese-words');
converter.toWords(13); // => “十三”

// Decimal numbers:
converter.toWords(2.9); // => “二點九”

// Negative numbers:
converter.toWords(-3); // => "負三"

// Large numbers:
converter.toWords(9007199254740992); // => “九千零七兆一千九百九十二億五千四百七十四萬零九百九十二”

toWordsOrdinal(number)

將整數轉換成文字,再加上前置的「第」字。 如果輸入的數字包含小數點,小數點後的數目將會被移除。

import converter from "number-to-chinese-words";
converter.toWordsOrdinal(21); // => “第二十一”

大寫

var converter = require("number-to-chinese-words")
converter.default.labels = Object.assign({},converter.default.labels, {
  digits : ['零','壹', '貳', '參', '肆', '伍', '陸', '柒', '捌', '玖'],
  units: ['','拾', '佰', '仟', '萬', '拾', '佰', '仟', '億', '拾', '佰', '仟', '兆', '拾', '佰', '仟', '京', '拾', '佰', '仟', '垓']
})

converter.toWords(199254740992); // => 壹仟玖佰玖拾貳億伍仟肆佰柒拾肆萬零玖佰玖拾貳

Direct load javascript from html

  <script src="//unpkg.com/number-to-chinese-words@^1.0/number-to-chinese-words.min.js"></script>
  <script>
    var converter = window.index.NumberToChineseWords;
    console.log(converter.toWords(13)); // => 十三
    console.log(converter.toWords(2.9)); // => 二點九
    console.log(converter.toWords(-3)); // => 負三
    console.log(converter.toWords(9007199254740992)); // => 九千零七兆一千九百九十二億五千四百七十四萬零九百九十二

    converter.labels = Object.assign({},converter.labels, {
      digits : ['零','壹', '貳', '參', '肆', '伍', '陸', '柒', '捌', '玖'],
      units: ['','拾', '佰', '仟', '萬', '拾', '佰', '仟', '億', '拾', '佰', '仟', '兆', '拾', '佰', '仟', '京', '拾', '佰', '仟', '垓']
    });

    console.log(converter.toWords(199254740992));// => 壹仟玖佰玖拾貳億伍仟肆佰柒拾肆萬零玖佰玖拾貳
  </script>

number-to-chinese-words's People

Contributors

digi3colin avatar tinac avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.