Hello
Today I am explain how to display amount in text in both gujrati and english.
<label class="col-sm-2 control-label" title="price">Ticket Price<span class="red">*</span></label>
<div class="col-sm-2" title="price">
<input type="number" class="form-control" name="ticketprice" id="ticketprice" placeholder="Enter ticket Price" onkeypress="return onlyNumberKey(event)">
<h5 id="pricemsg" style="color: red;">Please fill Ticket Price</h5>
</div>
<span class="col-sm-4 green" title="price">
<strong>
<span id="amountWords"></span><br>
<span id="amountWordsG"></span>
</strong>
</span>
</div>
JS code:
<script>
//gujrati
const gujaratiNumbers = [
"", "એક", "બે", "ત્રણ", "ચાર", "પાંચ", "છ", "સાત", "આઠ", "નવ",
"દસ", "અગિયાર", "બાર", "તેર", "ચૌદ", "પંદર", "સોળ", "સત્તર", "અઢાર", "ઓગણીસ",
"વીસ", "એકવીસ", "બાવીસ", "ત્રેવીસ", "ચોવીસ", "પચ્ચીસ", "છવીસ", "સત્તાવીસ", "અઠ્ઠાવીસ", "ઓગણત્રીસ", "ત્રીસ", "એકત્રીસ", "બત્રીસ", "તેત્રીસ", "ચોત્રીસ", "પાંત્રીસ", "છત્રીસ", "સાડત્રીસ", "આડત્રીસ", "ઓગણચાલીસ",
"ચાલીસ", "એકતાલીસ", "બેતાલીસ", "તેતાલીસ", "ચુંમાલીસ", "પિસ્તાલીસ", "છેતાલીસ", "સુડતાલીસ", "અડતાલીસ", "ઓગણપચાસ",
"પચાસ", "એકાવન", "બાવન", "તેપન", "ચોપન", "પંચાવન", "છપ્પન", "સત્તાવન", "અઠ્ઠાવન", "ઓગણસાઠ",
"સાઠ", "એકસઠ", "બાસઠ", "તેંસઠ", "ચોસઠ", "પાંસઠ", "છાંસઠ", "સડસઠ", "અડસઠ", "ઓગણસિત્તેર",
"સિત્તેર", "એકોતેર", "બોતેર", "તોતેર", "ચુમોતેર", "પંચોતેર", "છોતેર", "સિત્યોતેર", "ઇઠ્યોતેર", "ઓગણએંસી",
"એંસી", "એક્યાસી", "બ્યાસી", "ત્ર્યાસી", "ચોર્યાસી", "પંચ્યાસી", "છ્યાસી", "સિત્યાસી", "ઈઠ્યાસી", "નેવ્યાસી",
"નેવું", "એકાણું", "બાણું", "ત્રાણું", "ચોરાણું", "પંચાણું", "છન્નું", "સત્તાણું", "અઠાણું", "નવ્વાણું"
];
const gujaratiHundreds = {
1: "એકસો",
2: "બસો",
3: "ત્રણસો",
4: "ચારસો",
5: "પાંચસો",
6: "છસો",
7: "સાતસો",
8: "આઠસો",
9: "નવસો"
};
function numberToGujaratiWords(num) {
if (!num || num === 0) return "";
function convertBelow1000(n) {
let str = "";
if (n >= 100) {
const h = Math.floor(n / 100);
str += gujaratiHundreds[h] + " ";
n = n % 100;
}
if (n > 0) {
str += gujaratiNumbers[n];
}
return str.trim();
}
let result = "";
const crore = Math.floor(num / 10000000);
const lakh = Math.floor((num % 10000000) / 100000);
const thousand = Math.floor((num % 100000) / 1000);
const rest = num % 1000;
if (crore) result += convertBelow1000(crore) + " કરોડ ";
if (lakh) result += convertBelow1000(lakh) + " લાખ ";
if (thousand) result += convertBelow1000(thousand) + " હજાર ";
if (rest) result += convertBelow1000(rest);
return result.trim() + " રૂપિયા પુરા.";
}
//english
function numberToWords(num) {
num = parseInt(num);
if (isNaN(num)) return '';
const belowTwenty = ['', 'One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten',
'Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen','Seventeen','Eighteen','Nineteen'];
const tens = ['', '', 'Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'];
function convertHundreds(n) {
let str = '';
if (n > 99) {
str += belowTwenty[Math.floor(n / 100)] + ' Hundred ';
n = n % 100;
}
if (n > 19) {
str += tens[Math.floor(n / 10)] + ' ';
n = n % 10;
}
if (n > 0) {
str += belowTwenty[n] + ' ';
}
return str;
}
let result = '';
if (num >= 10000000) {
result += convertHundreds(Math.floor(num / 10000000)) + 'Crore ';
num = num % 10000000;
}
if (num >= 100000) {
result += convertHundreds(Math.floor(num / 100000)) + 'Lakh ';
num = num % 100000;
}
if (num >= 1000) {
result += convertHundreds(Math.floor(num / 1000)) + 'Thousand ';
num = num % 1000;
}
if (num > 0) {
result += convertHundreds(num);
}
return result.trim();
}
document.getElementById("ticketprice").addEventListener("input", function () {
let value = this.value;
if (value == "" || value == 0) {
document.getElementById("amountWords").innerText = "";
return;
}
document.getElementById("amountWords").innerText =
numberToWords(value) + " Rupees Only";
//gujrati
const output = $('#amountWordsG');
if (!isNaN(value) && value > 0) {
output.text(numberToGujaratiWords(value));
} else {
output.text('');
}
});
</script>