Created
November 13, 2017 04:32
-
-
Save adtjha/3a1ef8c53c09bd260c8c43fe13689697 to your computer and use it in GitHub Desktop.
Parse any number into its indian rupees format.
This file contains 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
function parse(n) { | |
var rounded= Math.round(n); | |
var value = rounded.toString(); | |
console.log(rounded, value, value.length); | |
var number = []; | |
var c=1; | |
var num = ''; | |
for (var i = 0; i < value.length; i++) { | |
number[i] = value.charAt(i); | |
if (i==0) { | |
console.log('creating a copy...'); | |
} | |
} | |
console.log(number); | |
for (var i = number.length-1; i >= 0; i--) { | |
if (i == number.length-1) { | |
console.log('Adding Commas...'); | |
} | |
num += number[i]; | |
if (i==number.length-3) { | |
num += ','; | |
} | |
if (i<=number.length-4) { | |
c++; | |
if(c%2==0){ | |
} | |
else | |
num+= ','; | |
} | |
} | |
console.log(num); | |
num = reve(num); | |
return num; | |
console.log('Original Number -----> '+value); | |
console.log('New Number -----> '+num); | |
function reve(n) { | |
var str = ''; | |
console.log('reversing...'); | |
for (var i = n.length-1; i >= 0; i--) { | |
if (i==n.length-1&& n.charAt(i)==',') { | |
console.log('Last Time Check for commas at the start...'); | |
continue; | |
} | |
str +=n.charAt(i); | |
} | |
console.log(str); | |
return str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Convert raw numbers into formated Indian currency.
Using this particular script is very simple.
Firstly add the above code in your javascript code and then just call the function like below, where num is the number you want to parse.
wherein the result variable will store the value on function call.It will return the formatted string.
Example:
var result = parse(1234567);
console.log(result);
Output/Console
Rs. 12,34,567
Created by @adtjha 👍