Created
June 14, 2019 07:14
-
-
Save yasserzubair/3e0c4a8d54b6e66554483b8ea6cac67d to your computer and use it in GitHub Desktop.
Javascript implementation for converting price to Pakistani/Indian rupees format strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function numToString(x) { | |
var r = 0; | |
var txter = x; | |
var sizer = txter.length; | |
var numStr = ""; | |
if (isNaN(txter)) { | |
return (" Invalid number"); | |
// exit(); | |
} | |
var n = parseInt(x); | |
var places = 0; | |
var str = ""; | |
var entry = 0; | |
while (n >= 1) { | |
r = parseInt(n % 10); | |
if (places < 3 && entry == 0) { | |
numStr = txter.substring(txter.length - 0, txter.length - 3) // Checks for 1 to 999. | |
if (numStr != '000') { | |
str = onlyDigit(numStr); | |
if(numStr[2] == '0') { | |
str = str + '0' | |
} | |
} | |
// str=onlyDigit(numStr); //Calls function for last 3 digits of the value. | |
entry = 1; | |
} | |
if (places == 3) { | |
numStr = txter.substring(txter.length - 5, txter.length - 3) | |
if (numStr != "" && numStr != "00") { | |
if (numStr[1] == '0' && numStr != '10') { | |
str = onlyDigit(numStr) + "0 Thousand " + str; | |
} else { | |
str = onlyDigit(numStr) + " Thousand " + str; | |
} | |
// str=onlyDigit(numStr)+ " Thousand "+str; | |
} | |
} | |
if (places == 5) { | |
numStr = txter.substring(txter.length - 7, txter.length - 5) //Substring for 5 place to 7 place of the string | |
if (numStr != "" && numStr != "00") { | |
if (numStr[1] == '0' && numStr != '10') { | |
str = onlyDigit(numStr) + "0 Lakhs " + str; | |
} else { | |
str = onlyDigit(numStr) + " Lakhs " + str; | |
} | |
// str=onlyDigit(numStr)+ " Lakhs "+str; //Appends the word lakhs to it | |
} | |
} | |
if (places == 6) { | |
numStr = txter.substring(txter.length - 9, txter.length - 7) //Substring for 7 place to 8 place of the string | |
if (numStr != "" && numStr != "00") { | |
if (numStr[1] == '0' && numStr != '10') { | |
str = onlyDigit(numStr) + "0 Crores " + str; | |
} else { | |
str = onlyDigit(numStr) + " Crores " + str; | |
} //Appends the word Crores | |
} | |
} | |
n = parseInt(n / 10); | |
places++; | |
} | |
return (str); | |
} | |
function onlyDigit(n) { | |
//Arrays to store the string equivalent of the number to convert in words | |
var units = ['', '1', '2', '3', '4', '5', '6', '7', '8', '9']; | |
var randomer = ['', '11', '12', '13', '14', '15', '16', '17', '18', '19']; | |
var tens = ['', '10', '2', '3', '4', '5', '6', '7', '8', '9']; | |
var r = 0; | |
var num = parseInt(n); | |
var str = ""; | |
var pl = ""; | |
var tenser = ""; | |
while (num >= 1) { | |
r = parseInt(num % 10); | |
tenser = r + tenser; | |
if (tenser <= 19 && tenser > 10) //Logic for 10 to 19 numbers | |
{ | |
str = randomer[tenser - 10]; | |
} | |
else { | |
if (pl == 0) //If units place then call units array. | |
{ | |
str = units[r]; | |
} | |
else if (pl == 1) //If tens place then call tens array. | |
{ | |
str = tens[r] + str; | |
} | |
} | |
if (pl == 2) //If hundreds place then call units array. | |
{ | |
str = units[r] + str; | |
} | |
num = parseInt(num / 10); | |
pl++; | |
} | |
return str; | |
} | |
// found a part of this on an online forum and modified it to fit to my needs. Fixed it for every scenario as far as i could. Works really good. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment