Giter VIP home page Giter VIP logo

php-number-to-word-india's Introduction

Number to Words PHP Class - Indian Version

Build Status Code Climate Test Coverage Issue Count

This class converts numbers to Indian-English Words. Works for all kinds of numbers including floats. A few examples.

Examples

  • 10,000,00 - Ten Lakh
  • 3,478 - Three Thousand Four Hundred And Seventy Eight
  • 1,234,567,890 - One Hundred And Twenty Three Crore Forty Five Lakh Sixty Seven Thousand Eight Hundred And Ninty
  • 5,024.78 - Five Thousand Twenty Four And 78/100

Installation

Using Composer

Navigate to your project directory and execute

composer require ntwindia/ntwindia

If you haven't already, include the composer autoload.php file.

require_once 'vendor/autoload.php';

Manual Installation

Clone this repository using git

git clone [email protected]:swashata/php-number-to-word-india.git

Include the class

<?php
require_once 'src/NTWIndia.php';
require_once 'src/Exception/NTWIndiaInvalidNumber.php';
require_once 'src/Exception/NTWIndiaNumberOverflow.php';

Usage

Create an instance

<?php
$ntw = new \NTWIndia\NTWIndia();

Convert values

<?php
echo $ntw->numToWord( 3104007200 );
// Will print Three Hundred Ten Crore Forty Lakh Seven Thousand Two Hundred

If your number is always less than 100, then use numToWordSmall method to reduce memory usage.

<?php
echo $ntw->numToWordSmall( 99 );
// Will print Ninty Nine

Methods

Two methods are available

numToWord

Converts any number to word including decimal values. Decimals are converted as ... And 986/1000. You will mostly need to call this method.

Parameters

  • $number: integer|float The number whose word value would be generated.

Return

Returns string The word value of the number. All of the words have uppercased first letter.

numToWordSmall

Converts numbers smaller than 100 into words. If the number is greater than 99 then it simply calls numToWord.

Use this when you know your number is lesser than 100 to reduce memory usage.

Parameters

  • $number: integer The number whose word value would be generated. It does not accepts floating values and will convert to integer if given.

Return

Returns string The word value of the number. All of the words have uppercased first letter.

Exception Handling

Two exceptions are thrown depending on the condition.

\NTWIndia\Exception\NTWIndiaInvalidNumber

If you pass a variable to a method which isn't a valid number ( ! is_numeric( $number ) )

\NTWIndia\Exception\NTWIndiaNumberOverflow

If pass a number which exceeds the limit.

  • For numToWord it is the PHP_MAX_INT.
  • For numToWordSmall it is 99.

Translation

To translate, kindly replace the public variables:

  • $hundred: Hundred word. Defaults 'Hundred'.
  • $thousand: Thousand word. Defaults 'Thousand'.
  • $lakh: Lakh word. Defaults 'Lakh'.
  • $crore: Crore word. Defaults 'Crore'.
  • $and: And word. Defaults 'And'.
  • $numToWord: Contains mapping of numbers to words. From 0 to 20 and multiple of 10 upto 90.

Unit Test

Unit test is available through composer.

$ cd /path/to/php-number-to-word-india/
$ composer update
$ vendor/bin/phpunit

It will output something like this.

Test Case

You are more than welcome to add more test cases and send me a pull request.

Kindly follow Contribution Guide

License

PHP Number to Word in Indian Style is licensed under GPLv3

Copyright (C) 2017  Swashata Ghosh

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>

php-number-to-word-india's People

Contributors

swashata avatar

Stargazers

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

Watchers

 avatar  avatar  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.