Comparing version 0.0.2 to 0.1.0
@@ -7,1 +7,4 @@ const pnumber = require('./index') | ||
console.log(pnumber.toWordTomans(number)) | ||
console.log(pnumber.toPersianDigits(number)) | ||
console.log(pnumber.toEnglishDigits('۱۲۳۴')) | ||
console.log(pnumber.toEnglishDigits('۶۳ ن ۵۵۷ ایران ۱۱')) |
58
index.js
@@ -7,7 +7,9 @@ 'use strict'; | ||
if (num < 0) { | ||
num = num * -1; | ||
return "منفی " + toWord(num, level); | ||
let _num = parseInt(toEnglishDigits(num.toString())) | ||
if (_num < 0) { | ||
_num = _num * -1; | ||
return "منفی " + toWord(_num, level); | ||
} | ||
if (num === 0) { | ||
if (_num === 0) { | ||
if (level === 0) { | ||
@@ -29,18 +31,18 @@ return "صفر"; | ||
if (num < 10) { | ||
result += yekan[num - 1]; | ||
} else if (num < 20) { | ||
result += dah[num - 10]; | ||
} else if (num < 100) { | ||
result += dahgan[parseInt(num / 10, 10) - 2] + toWord(num % 10, level + 1); | ||
} else if (num < 1000) { | ||
result += sadgan[parseInt(num / 100, 10) - 1] + toWord(num % 100, level + 1); | ||
} else if (num < 1000000) { | ||
result += toWord(parseInt(num / 1000, 10), level) + " هزار " + toWord(num % 1000, level + 1); | ||
} else if (num < 1000000000) { | ||
result += toWord(parseInt(num / 1000000, 10), level) + " میلیون " + toWord(num % 1000000, level + 1); | ||
} else if (num < 1000000000000) { | ||
result += toWord(parseInt(num / 1000000000, 10), level) + " میلیارد " + toWord(num % 1000000000, level + 1); | ||
} else if (num < 1000000000000000) { | ||
result += toWord(parseInt(num / 1000000000000, 10), level) + " تریلیارد " + toWord(num % 1000000000000, level + 1); | ||
if (_num < 10) { | ||
result += yekan[_num - 1]; | ||
} else if (_num < 20) { | ||
result += dah[_num - 10]; | ||
} else if (_num < 100) { | ||
result += dahgan[parseInt(_num / 10, 10) - 2] + toWord(_num % 10, level + 1); | ||
} else if (_num < 1000) { | ||
result += sadgan[parseInt(_num / 100, 10) - 1] + toWord(_num % 100, level + 1); | ||
} else if (_num < 1000000) { | ||
result += toWord(parseInt(_num / 1000, 10), level) + " هزار " + toWord(_num % 1000, level + 1); | ||
} else if (_num < 1000000000) { | ||
result += toWord(parseInt(_num / 1000000, 10), level) + " میلیون " + toWord(_num % 1000000, level + 1); | ||
} else if (_num < 1000000000000) { | ||
result += toWord(parseInt(_num / 1000000000, 10), level) + " میلیارد " + toWord(_num % 1000000000, level + 1); | ||
} else if (_num < 1000000000000000) { | ||
result += toWord(parseInt(_num / 1000000000000, 10), level) + " تریلیارد " + toWord(_num % 1000000000000, level + 1); | ||
} | ||
@@ -67,2 +69,16 @@ return result; | ||
const toEnglishDigits = function (str) { | ||
var charCodeZero = '۰'.charCodeAt(0); | ||
return str.replace(/[۰-۹]/g, function (w) { | ||
return w.charCodeAt(0) - charCodeZero; | ||
}); | ||
} | ||
const toPersianDigits = function (str) { | ||
const persian={0:'۰',1:'۱',2:'۲',3:'۳',4:'۴',5:'۵',6:'۶',7:'۷',8:'۸',9:'۹'}; | ||
return str.replace(/[0-9]/g, function (w) { | ||
return persian[w]; | ||
}); | ||
} | ||
if (typeof module !== 'undefined' && module.exports) { | ||
@@ -72,2 +88,4 @@ module.exports.toWord = toWord; | ||
module.exports.toWordTomans = toWordTomans; | ||
module.exports.toPersianDigits = toPersianDigits; | ||
module.exports.toEnglishDigits = toEnglishDigits; | ||
} |
{ | ||
"name": "pnumber", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "persian number utilities for javascript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# pnumber | ||
persian number utilities for javascript | ||
## features | ||
+ Convert number to word | ||
+ Convert number to word in Rials | ||
+ Convert number to word in Tomans | ||
+ Convert persian number to english number in string | ||
+ Convert english number to persian number in string | ||
@@ -15,2 +21,5 @@ ## Usage | ||
console.log(pnumber.toWordTomans(number)) | ||
console.log(pnumber.toPersianDigits(number)) | ||
console.log(pnumber.toEnglishDigits('۱۲۳۴')) | ||
console.log(pnumber.toEnglishDigits('۶۳ ن ۵۵۷ ایران ۱۱')) | ||
@@ -22,2 +31,5 @@ +++++++++ | ||
دوازده هزار و سیصد و چهل و پنج تومان | ||
۱۲۳۴۵۶ | ||
1234 | ||
63 ن 557 ایران 11 | ||
``` | ||
@@ -27,3 +39,9 @@ | ||
``` | ||
import { toWord, toWordRials, toWordTomans } from 'pnumber' | ||
import { | ||
toWord, | ||
toWordRials, | ||
toWordTomans, | ||
toEnglishDigits, | ||
toPersianDigits | ||
} from 'pnumber' | ||
@@ -34,2 +52,5 @@ const number = '123456' | ||
console.log(toWordTomans(number)) | ||
console.log(toPersianDigits(number)) | ||
console.log(toEnglishDigits('۱۲۳۴')) | ||
console.log(toEnglishDigits('۶۳ ن ۵۵۷ ایران ۱۱')) | ||
@@ -42,2 +63,5 @@ +++++++++ | ||
دوازده هزار و سیصد و چهل و پنج تومان | ||
۱۲۳۴۵۶ | ||
1234 | ||
63 ن 557 ایران 11 | ||
``` |
6587
85
63