├── .gitignore ├── .npmignore ├── .prettierrc ├── .vscode └── settings.json ├── LICENSE ├── README.md ├── dist ├── index.js ├── rials.js ├── stringify.js └── tomans.js ├── index.js ├── package.json └── src └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /**/node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /**/build/** 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | 25 | /build 26 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .github 2 | 3 | docs 4 | 5 | node_modules 6 | 7 | src 8 | 9 | .gitignore 10 | 11 | .prettierrc 12 | 13 | babel.config.json 14 | 15 | index.test.js 16 | 17 | package-lock.json 18 | 19 | coverage 20 | 21 | src -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true 3 | } 4 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.tabSize": 2, 3 | "editor.formatOnSave": true 4 | } 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Mehdi Ardakani 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Stringify Numbers 2 | 3 | It is a package for converting numbers to letters, which currently only supports the Persian language, and other languages will be added in the future. 4 | 5 | # Installation 6 | 7 | ``` 8 | npm i stringify-numbers 9 | ``` 10 | 11 | # Usage 12 | 13 | ```js 14 | import { stringifyFa } from 'stringify-numbers'; 15 | 16 | stringifyFa(125325652); 17 | 18 | //return دوازده میلیون و پانصد و سی و دو هزار و پانصد و شصت و پنج 19 | ``` 20 | 21 | # Use different modes 22 | 23 | ### Use Toman 24 | 25 | ```js 26 | import { stringifyTomans } from 'stringify-numbers'; 27 | 28 | stringifyTomans(125325652); 29 | 30 | //return دوازده میلیون و پانصد و سی و دو هزار و پانصد و شصت و پنج تومان 31 | ``` 32 | 33 | ### Use Rial 34 | 35 | ```js 36 | import { stringifyRials } from 'stringify-numbers'; 37 | 38 | stringifyRials(125325652); 39 | 40 | //return یکصد و بیست و پنج میلیون و سیصد و بیست و پنج هزار و ششصد و پنجاه و دو ریال 41 | ``` 42 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | export { stringifyFa } from './dist/stringify'; 2 | 3 | export { stringifyTomans } from './dist/tomans'; 4 | 5 | export { stringifyRials } from './dist/rials'; 6 | -------------------------------------------------------------------------------- /dist/rials.js: -------------------------------------------------------------------------------- 1 | import { stringifyFa } from "./stringify"; 2 | 3 | export const stringifyRials = function (num) { 4 | 'use strict'; 5 | return stringifyFa(num, 0) + " ریال"; 6 | }; 7 | -------------------------------------------------------------------------------- /dist/stringify.js: -------------------------------------------------------------------------------- 1 | export const stringifyFa = function (num, level) { 2 | "use strict"; 3 | 4 | function isCorrectNumber(num) { 5 | return /^-?(\d{1,3},?)+(\.?\d+)?$/.test(num); 6 | } 7 | 8 | if (num === null) { 9 | return ""; 10 | } 11 | 12 | level = level || 0; 13 | 14 | // remove all non digits from string 15 | if (level === 0 && typeof num === "string" && isCorrectNumber(num)) { 16 | num = parseInt(num.replace(/,/g, "")); 17 | } 18 | 19 | // convert negative number to positive and get wordify value 20 | if (num < 0) { 21 | num = num * -1; 22 | return "منفی " + stringifyFa(num, level); 23 | } 24 | if (num === 0) { 25 | if (level === 0) { 26 | return "صفر"; 27 | } else { 28 | return ""; 29 | } 30 | } 31 | var result = "", 32 | yekan = [ 33 | " یک ", 34 | " دو ", 35 | " سه ", 36 | " چهار ", 37 | " پنج ", 38 | " شش ", 39 | " هفت ", 40 | " هشت ", 41 | " نه ", 42 | ], 43 | dahgan = [ 44 | " بیست ", 45 | " سی ", 46 | " چهل ", 47 | " پنجاه ", 48 | " شصت ", 49 | " هفتاد ", 50 | " هشتاد ", 51 | " نود ", 52 | ], 53 | sadgan = [ 54 | " یکصد ", 55 | " دویست ", 56 | " سیصد ", 57 | " چهارصد ", 58 | " پانصد ", 59 | " ششصد ", 60 | " هفتصد ", 61 | " هشتصد ", 62 | " نهصد ", 63 | ], 64 | dah = [ 65 | " ده ", 66 | " یازده ", 67 | " دوازده ", 68 | " سیزده ", 69 | " چهارده ", 70 | " پانزده ", 71 | " شانزده ", 72 | " هفده ", 73 | " هیجده ", 74 | " نوزده ", 75 | ]; 76 | if (level > 0) { 77 | result += " و "; 78 | level -= 1; 79 | } 80 | 81 | if (num < 10) { 82 | result += yekan[num - 1]; 83 | } else if (num < 20) { 84 | result += dah[num - 10]; 85 | } else if (num < 100) { 86 | result += 87 | dahgan[parseInt(num / 10, 10) - 2] + stringifyFa(num % 10, level + 1); 88 | } else if (num < 1000) { 89 | result += 90 | sadgan[parseInt(num / 100, 10) - 1] + stringifyFa(num % 100, level + 1); 91 | } else if (num < 1000000) { 92 | result += 93 | stringifyFa(parseInt(num / 1000, 10), level) + 94 | " هزار " + 95 | stringifyFa(num % 1000, level + 1); 96 | } else if (num < 1000000000) { 97 | result += 98 | stringifyFa(parseInt(num / 1000000, 10), level) + 99 | " میلیون " + 100 | stringifyFa(num % 1000000, level + 1); 101 | } else if (num < 1000000000000) { 102 | result += 103 | stringifyFa(parseInt(num / 1000000000, 10), level) + 104 | " میلیارد " + 105 | stringifyFa(num % 1000000000, level + 1); 106 | } else if (num < 1000000000000000) { 107 | result += 108 | stringifyFa(parseInt(num / 1000000000000, 10), level) + 109 | " تریلیارد " + 110 | stringifyFa(num % 1000000000000, level + 1); 111 | } 112 | return result; 113 | }; 114 | -------------------------------------------------------------------------------- /dist/tomans.js: -------------------------------------------------------------------------------- 1 | import { stringifyFa } from "stringify/dist/stringify"; 2 | 3 | export const stringifyTomans = function (num) { 4 | 'use strict'; 5 | if (num >= 10) { 6 | num = parseInt(num / 10, 10); 7 | } else if (num <= -10) { 8 | num = parseInt(num / 10, 10); 9 | } else { 10 | num = 0; 11 | } 12 | 13 | return stringifyFa(num, 0) + " تومان"; 14 | }; -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export { stringifyFa } from './dist/stringify'; 2 | 3 | export { stringifyTomans } from './dist/tomans'; 4 | 5 | export { stringifyRials } from './dist/rials'; 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "license": "MIT", 3 | "main": "index.js", 4 | "name": "stringify-number", 5 | "types": "=index.js", 6 | "version": "0.1.0", 7 | "scripts": { 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/mehdi-ard/stringify.git" 13 | }, 14 | "keywords": [ 15 | "javascript", 16 | "react", 17 | "vue", 18 | "angular", 19 | "node.js", 20 | "convert string", 21 | "stringify", 22 | "convert number" 23 | ], 24 | "author": "", 25 | "bugs": { 26 | "url": "https://github.com/mehdi-ard/stringify/issues" 27 | }, 28 | "homepage": "https://github.com/mehdi-ard/stringify#readme" 29 | } 30 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import {} from '../dist'; 4 | import { stringifyRials, stringifyFa, stringifyTomans } from '../dist'; 5 | import './index.css'; 6 | 7 | const App = () => { 8 | const priceToman = 120000000; 9 | const priceRial = 116223854; 10 | const number = 125948; 11 | 12 | return ( 13 | <> 14 |

{stringifyFa(number)}

15 | 16 |

{stringifyRials(priceRial)}

17 | 18 |

{stringifyTomans(priceToman)}

19 | 20 | ); 21 | }; 22 | ReactDOM.render(, document.getElementById('container')); 23 | --------------------------------------------------------------------------------