Giter VIP home page Giter VIP logo

to-words's Introduction

Number to Words

Introduction

Converts Numbers (including decimal points) into words. It also converts the numbers into words for currency.

Installation

npm install to-words --save

Usage

Importing

const { ToWords } = require('to-words');

OR

import { ToWords } from 'to-words';

Config Options

const toWords = new ToWords({
  localeCode: 'en-IN',
  converterOptions: {
    currency: true,
    ignoreDecimal: false,
    ignoreZeroCurrency: false,
    doNotAddOnly: false,
    currencyOptions: {
      // can be used to override defaults for the selected locale
      name: 'Rupee',
      plural: 'Rupees',
      symbol: 'โ‚น',
      fractionalUnit: {
        name: 'Paisa',
        plural: 'Paise',
        symbol: '',
      },
    },
  },
});

Options can be set at instance level, or along with individual call to convert method.

const toWords = new ToWords();

let words = toWords.convert(123);
// words = One Hundred Twenty Three

words = toWords.convert(123.45);
// words = One Hundred Twenty Three Point Fourty Five

words = toWords.convert(123.045);
// words = One Hundred Twenty Three Point Zero Four Five

Note: When fractional part starts with zero, the digits after decimal points are converted into respective numbers individually

To convert to currency

const toWords = new ToWords();

let words = toWords.convert(452, { currency: true });
// words = Four Hundred Fifty Two Rupees Only

words = toWords.convert(452.36, { currency: true });
// words = Four Hundred Fifty Two Rupees And Thirty Six Paise Only

To discard fractional unit

const toWords = new ToWords();

let words = toWords.convert(452.36, { currency: true, ignoreDecimal: true });
// words = Four Hundred Fifty Two Rupees Only

To ignore major currency number when it's zero

const toWords = new ToWords();

let words = toWords.convert(0.572, { currency: true, ignoreZeroCurrency: true });
// words = Five Hundred Seventy Two Paise Only

Options

Option Type Default Description
localeCode string 'en-IN' Locale code for selecting i18n.
currency boolean false Whether the number to be converted into words written as currency.
Note: When currency:true, number will be rounded off to two decimals before converting to words
ignoreDecimal boolean false Whether to ignore fractional unit of number while converting into words.
ignoreZeroCurrency boolean false Whether to ignore zero currency value while converting into words.
doNotAddOnly boolean false Do not add only at the end of the words. This works only when currency = true
currencyOptions object undefined By default currency options are taken from the specified locale.
This option allows to specify different currency options while keeping the language details from the selected locale (e.g. convert to English text but use EUR as a currency). You can define different currencies for each call to convert() so it works also if you need to dynamically support multiple currencies.
This works only when currency = true

Supported Locale

Country Language Locale
UAE English en-AE
Bangladesh English en-BD
UK English en-GB
Ghana English en-GH
Ireland English en-IE
India English en-IN (default)
Myanmar English en-MM
Mauritius English en-MU
Nigeria English en-NG
Nepal English en-NP
USA English en-US
Philippines English en-PH
Estonia Estonian ee-EE
Iran Persian fa-IR
Belgium French fr-BE
France French fr-FR
India Gujarati gu-IN
India Hindi hi-IN
India Marathi mr-IN
Suriname Dutch nl-SR
Brazil Portuguese pt-BR
Turkey Turkish tr-TR
Korean, Republic of Hangul ko-KR

Inspiration for core logic

https://stackoverflow.com/a/46221860

to-words's People

Contributors

actav-n avatar akoso avatar blue-predator avatar dalirnet avatar deepsourcebot avatar dependabot-preview[bot] avatar dependabot[bot] avatar dueka avatar eta1993 avatar fijinapdt avatar florianchappaz avatar hardiksondagar avatar masihjahangiri avatar mastermunj avatar ohmmolina avatar rhyso avatar rittme avatar rubek-joshi avatar s1gtrap avatar saifergit avatar sylvebois avatar wocodes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

to-words's Issues

Pound and Euro currency required

hi am sagar, am updating changes and committing code, but code not committed through git

subject and type missing like error showing

Locale Support

Hi Tean,

We are using this package for currency conversion. we are facing challenge regarding locale. As per my requirements, we need to support below locales

  1. ar-DZ
  2. en-UK
  3. ja-JP
  4. th-TH
  5. zh-CN

this package is not supporting above locales. Please guide us how to implement this locale support using this package.

Types files are missing from Typescript compilation

I tried using this module in Angular project. When I build my project its throwing error for types files.

WARNING in ./node_modules/to-words/dist/locales/en-US.d.ts
Module build failed (from ./node_modules/@ngtools/webpack/src/index.js):
Error: /var/lib/name/node_modules/to-words/dist/locales/en-US.d.ts is missing from the TypeScript compilation. Please make sure it is
in your tsconfig via the 'files' or 'include' property.

Tried adding the files in tsconfig but still the error is same.

image

Procedure to Replicate Issue

  1. Create new angular project and install to-words library

  2. Chnage app.component.ts like this

import { Component, OnInit } from '@angular/core';
import { ToWords } from "to-words";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'to-words-test';

  ngOnInit() {
    const toWords = new ToWords({
      localeCode: 'en-IN', converterOptions: {
        currency: true
      }
    });
    const word = toWords.convert(1234, { currency: true });
    console.log(word);
  }
}
  1. Do ng build or ng serve which will show the error

Arabic Support

Hello Guys ๐Ÿ˜.
I'm wondering if can I add the arabic support to the package ?
If yes, Please confirm so I can open my PR.

Thanks!

Currency Name/Symbol's position in words

It would be nice if you could add in 'converterOptions' an option to display currency name/symbol Infront of words instead of in-between. Plural can be ignored here.
eg: INR Three Million Only, USD One Hundred And Sixty Cents Only etc...

Getting NaN Error

image

i keep getting this error on Edge browser
But i dont get it on chrome

Myanmar Kyats(MMK) not supporting

I have tired MMK , but this to-words package is not supporting MMK, pls add this MMK currency also for this package

ex: when we giving amount as 887.00 in digit

 it should convert like : -  eight hundred eighty seven Kyats only

name in currencyOptions not working

I tried below code, it works well but name config is not working. It always considers plural

{
    "localeCode": "en-IN",
    "converterOptions": {
        "currency": true,
        "currencyOptions": {
            "name": "Rupee",
            "plural": "Rupees",
            "symbol": "",
            "fractionalUnit": {
                "name": "Paisa",
                "plural": "Paise",
                "symbol": ""
            }
        }
    }
}

Test#1: 1.01 outputs One Rupees And One Paise Only but should be One Rupee And One Paisa Only
Test#2: 1.00 outputs One Rupees Only but should be One Rupee Only

Can you please check this or am I missing anything ?

Add Support for Ghana Locale

Hi, I'm super happy there is support for the Nigerian currency as I'm Nigerian but in my project I currently support users in Ghana and as such I'd love to have this support. I code in Javascript and have little to no knowledge of Typescript else, I'd love to contribute this

import { LocaleInterface } from './locale.interface'; > export declare class Locale implements LocaleInterface { | currency: { | name: string;

import { LocaleInterface } from './locale.interface';

export declare class Locale implements LocaleInterface {
| currency: {
| name: string;
import { LocaleInterface } from './locale.interface';
export declare class Locale implements LocaleInterface {
| currency: {
| name: string;

your library is awesome it solved my issue and lot of custom code but am getting this console message am using it part of the CRA create react app in my project

Add support for Spanish

It would be useful to have support for Spanish.

Currently we are using numero-palabra for this, but it would be good to have support as part of this package.

Currency Grammar

When currency flag is enabled and Math.floor(number) === 1. The plural currency is used instead of the singular form

1= 1 dollars
1.5 = 1 dollars and 5 cents

MMK does n't support Crore (number in words)

Hi @mastermunj mmk does not support crore pls replace crore with million

ex: public numberwordsMapping = [

                  {number:1000000, value: 'Million'},
                 {number: 100000,value:'Lakh'},
                 {number: 1000, value: 'Thousand'},
                {number:100,value:'hundred'},
                {number :10,value:'Ten'}

];

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.