How to Display Number Value in Words

how to display number value in words

So how the query works? Well here’s why:

select to_char(to_date(:number,'j'),'jsp') from dual;

If you look into the inner most part of the query to_date(:number,'j') the ‘j’ or J is the Julian Date (January 1, 4713 BC), basically this date is been used for astronomical studies.

So to_date(:number,'j') it take the number represented by number and pretend it is a julian date, convert into a date.

If you pass 3 to number, so it will convert date to 3rd Jan 4713 BC, it means 3 is added to the Julian date.

select to_char(to_date(3,'j'),'jsp') from dual;

Now to_char(to_date(3,'j'),'jsp'), jsp = Now; take that date(to_date(3,'j')) and spell the julian number it represents, the output is:

TO_CH
-----
three

There is a limitation while using Julian dates ,It ranges from 1 to 5373484. That’s why if you put the values after 5373484, it will throw you an error as shown below:

ORA-01854: julian date must be between 1 and 5373484

Hi everyone, it is interesting this topic. I remember when I was learning Oracle in 2005 one of the instructor required me to write a PL/SQL code to convert numbers in words, it was a whole two pages code to reach this.

Here is some reference that could help us to understand the Julian day, that is why we use the letter 'j' or 'J' during this operation.

First there is a website that has the example and explanation about "How To Convert Number Into Words Using Oracle SQL Query":

http://viralpatel.net/blogs/convert-number-into-words-oracle-sql-query/

Second if you want to know more about "Julian day" go to:

http://en.wikipedia.org/wiki/Julian_day

Third if you want to know more about who proposed the Julian day number in 1583, it was by "Joseph Scaliger":

http://en.wikipedia.org/wiki/Joseph_Justus_Scaliger

It is not make sence for me continue repiting what another author in these websites have made, that is why I just posted the link you can access them and read what you need to understand how query like this works:

SELECT TO_CHAR (TO_DATE (2447834, 'j'), 'jsp') FROM DUAL;

//Output: two million four hundred forty-seven thousand eight hundred thirty-four

how to display number value in words and decimals

A simple example, which checks whether there are any decimals. If so, they are spelled separately.

SQL> with test (col) as
2 (select 4432 from dual union all
3 select 515.24 from dual
4 ),
5 inter as
6 (select col,
7 regexp_substr(col, '^\d+') fir,
8 case when col <> round(col) then regexp_substr(col, '\d+$') end sec
9 from test
10 )
11 select col,
12 to_char(to_date(fir, 'J'), 'JSP') ||
13 case when sec is not null then ' and ' || to_char(to_date(sec, 'J'), 'JSP') ||' cents' end spell
14 from inter;

COL SPELL
---------- --------------------------------------------------
4432 FOUR THOUSAND FOUR HUNDRED THIRTY-TWO
515,24 FIVE HUNDRED FIFTEEN and TWENTY-FOUR cents

SQL>

How to convert number to words in java

Here is the code, I don't think there is any method in SE.

It basically converts number to string and parses String and associates it with the weight

for example

1000

1 is treated as thousand position and 1 gets mapped to "one" and thousand because of position


This is the code from the website:

English

import java.text.DecimalFormat;

public class EnglishNumberToWords {

private static final String[] tensNames = {
"",
" ten",
" twenty",
" thirty",
" forty",
" fifty",
" sixty",
" seventy",
" eighty",
" ninety"
};

private static final String[] numNames = {
"",
" one",
" two",
" three",
" four",
" five",
" six",
" seven",
" eight",
" nine",
" ten",
" eleven",
" twelve",
" thirteen",
" fourteen",
" fifteen",
" sixteen",
" seventeen",
" eighteen",
" nineteen"
};

private EnglishNumberToWords() {}

private static String convertLessThanOneThousand(int number) {
String soFar;

if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;

soFar = tensNames[number % 10] + soFar;
number /= 10;
}
if (number == 0) return soFar;
return numNames[number] + " hundred" + soFar;
}

public static String convert(long number) {
// 0 to 999 999 999 999
if (number == 0) { return "zero"; }

String snumber = Long.toString(number);

// pad with "0"
String mask = "000000000000";
DecimalFormat df = new DecimalFormat(mask);
snumber = df.format(number);

// XXXnnnnnnnnn
int billions = Integer.parseInt(snumber.substring(0,3));
// nnnXXXnnnnnn
int millions = Integer.parseInt(snumber.substring(3,6));
// nnnnnnXXXnnn
int hundredThousands = Integer.parseInt(snumber.substring(6,9));
// nnnnnnnnnXXX
int thousands = Integer.parseInt(snumber.substring(9,12));

String tradBillions;
switch (billions) {
case 0:
tradBillions = "";
break;
case 1 :
tradBillions = convertLessThanOneThousand(billions)
+ " billion ";
break;
default :
tradBillions = convertLessThanOneThousand(billions)
+ " billion ";
}
String result = tradBillions;

String tradMillions;
switch (millions) {
case 0:
tradMillions = "";
break;
case 1 :
tradMillions = convertLessThanOneThousand(millions)
+ " million ";
break;
default :
tradMillions = convertLessThanOneThousand(millions)
+ " million ";
}
result = result + tradMillions;

String tradHundredThousands;
switch (hundredThousands) {
case 0:
tradHundredThousands = "";
break;
case 1 :
tradHundredThousands = "one thousand ";
break;
default :
tradHundredThousands = convertLessThanOneThousand(hundredThousands)
+ " thousand ";
}
result = result + tradHundredThousands;

String tradThousand;
tradThousand = convertLessThanOneThousand(thousands);
result = result + tradThousand;

// remove extra spaces!
return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
}

/**
* testing
* @param args
*/
public static void main(String[] args) {
System.out.println("*** " + EnglishNumberToWords.convert(0));
System.out.println("*** " + EnglishNumberToWords.convert(1));
System.out.println("*** " + EnglishNumberToWords.convert(16));
System.out.println("*** " + EnglishNumberToWords.convert(100));
System.out.println("*** " + EnglishNumberToWords.convert(118));
System.out.println("*** " + EnglishNumberToWords.convert(200));
System.out.println("*** " + EnglishNumberToWords.convert(219));
System.out.println("*** " + EnglishNumberToWords.convert(800));
System.out.println("*** " + EnglishNumberToWords.convert(801));
System.out.println("*** " + EnglishNumberToWords.convert(1316));
System.out.println("*** " + EnglishNumberToWords.convert(1000000));
System.out.println("*** " + EnglishNumberToWords.convert(2000000));
System.out.println("*** " + EnglishNumberToWords.convert(3000200));
System.out.println("*** " + EnglishNumberToWords.convert(700000));
System.out.println("*** " + EnglishNumberToWords.convert(9000000));
System.out.println("*** " + EnglishNumberToWords.convert(9001000));
System.out.println("*** " + EnglishNumberToWords.convert(123456789));
System.out.println("*** " + EnglishNumberToWords.convert(2147483647));
System.out.println("*** " + EnglishNumberToWords.convert(3000000010L));

/*
*** zero
*** one
*** sixteen
*** one hundred
*** one hundred eighteen
*** two hundred
*** two hundred nineteen
*** eight hundred
*** eight hundred one
*** one thousand three hundred sixteen
*** one million
*** two millions
*** three millions two hundred
*** seven hundred thousand
*** nine millions
*** nine millions one thousand
*** one hundred twenty three millions four hundred
** fifty six thousand seven hundred eighty nine
*** two billion one hundred forty seven millions
** four hundred eighty three thousand six hundred forty seven
*** three billion ten
**/
}
}

Français
Quite different than the english version but french is a lot more difficult!

package com.rgagnon.howto;

import java.text.*;

class FrenchNumberToWords {
private static final String[] dizaineNames = {
"",
"",
"vingt",
"trente",
"quarante",
"cinquante",
"soixante",
"soixante",
"quatre-vingt",
"quatre-vingt"
};

private static final String[] uniteNames1 = {
"",
"un",
"deux",
"trois",
"quatre",
"cinq",
"six",
"sept",
"huit",
"neuf",
"dix",
"onze",
"douze",
"treize",
"quatorze",
"quinze",
"seize",
"dix-sept",
"dix-huit",
"dix-neuf"
};

private static final String[] uniteNames2 = {
"",
"",
"deux",
"trois",
"quatre",
"cinq",
"six",
"sept",
"huit",
"neuf",
"dix"
};

private FrenchNumberToWords() {}

private static String convertZeroToHundred(int number) {

int laDizaine = number / 10;
int lUnite = number % 10;
String resultat = "";

switch (laDizaine) {
case 1 :
case 7 :
case 9 :
lUnite = lUnite + 10;
break;
default:
}

// séparateur "-" "et" ""
String laLiaison = "";
if (laDizaine > 1) {
laLiaison = "-";
}
// cas particuliers
switch (lUnite) {
case 0:
laLiaison = "";
break;
case 1 :
if (laDizaine == 8) {
laLiaison = "-";
}
else {
laLiaison = " et ";
}
break;
case 11 :
if (laDizaine==7) {
laLiaison = " et ";
}
break;
default:
}

// dizaines en lettres
switch (laDizaine) {
case 0:
resultat = uniteNames1[lUnite];
break;
case 8 :
if (lUnite == 0) {
resultat = dizaineNames[laDizaine];
}
else {
resultat = dizaineNames[laDizaine]
+ laLiaison + uniteNames1[lUnite];
}
break;
default :
resultat = dizaineNames[laDizaine]
+ laLiaison + uniteNames1[lUnite];
}
return resultat;
}

private static String convertLessThanOneThousand(int number) {

int lesCentaines = number / 100;
int leReste = number % 100;
String sReste = convertZeroToHundred(leReste);

String resultat;
switch (lesCentaines) {
case 0:
resultat = sReste;
break;
case 1 :
if (leReste > 0) {
resultat = "cent " + sReste;
}
else {
resultat = "cent";
}
break;
default :
if (leReste > 0) {
resultat = uniteNames2[lesCentaines] + " cent " + sReste;
}
else {
resultat = uniteNames2[lesCentaines] + " cents";
}
}
return resultat;
}

public static String convert(long number) {
// 0 à 999 999 999 999
if (number == 0) { return "zéro"; }

String snumber = Long.toString(number);

// pad des "0"
String mask = "000000000000";
DecimalFormat df = new DecimalFormat(mask);
snumber = df.format(number);

// XXXnnnnnnnnn
int lesMilliards = Integer.parseInt(snumber.substring(0,3));
// nnnXXXnnnnnn
int lesMillions = Integer.parseInt(snumber.substring(3,6));
// nnnnnnXXXnnn
int lesCentMille = Integer.parseInt(snumber.substring(6,9));
// nnnnnnnnnXXX
int lesMille = Integer.parseInt(snumber.substring(9,12));

String tradMilliards;
switch (lesMilliards) {
case 0:
tradMilliards = "";
break;
case 1 :
tradMilliards = convertLessThanOneThousand(lesMilliards)
+ " milliard ";
break;
default :
tradMilliards = convertLessThanOneThousand(lesMilliards)
+ " milliards ";
}
String resultat = tradMilliards;

String tradMillions;
switch (lesMillions) {
case 0:
tradMillions = "";
break;
case 1 :
tradMillions = convertLessThanOneThousand(lesMillions)
+ " million ";
break;
default :
tradMillions = convertLessThanOneThousand(lesMillions)
+ " millions ";
}
resultat = resultat + tradMillions;

String tradCentMille;
switch (lesCentMille) {
case 0:
tradCentMille = "";
break;
case 1 :
tradCentMille = "mille ";
break;
default :
tradCentMille = convertLessThanOneThousand(lesCentMille)
+ " mille ";
}
resultat = resultat + tradCentMille;

String tradMille;
tradMille = convertLessThanOneThousand(lesMille);
resultat = resultat + tradMille;

return resultat;
}

public static void main(String[] args) {
System.out.println("*** " + FrenchNumberToWords.convert(0));
System.out.println("*** " + FrenchNumberToWords.convert(9));
System.out.println("*** " + FrenchNumberToWords.convert(19));
System.out.println("*** " + FrenchNumberToWords.convert(21));
System.out.println("*** " + FrenchNumberToWords.convert(28));
System.out.println("*** " + FrenchNumberToWords.convert(71));
System.out.println("*** " + FrenchNumberToWords.convert(72));
System.out.println("*** " + FrenchNumberToWords.convert(80));
System.out.println("*** " + FrenchNumberToWords.convert(81));
System.out.println("*** " + FrenchNumberToWords.convert(89));
System.out.println("*** " + FrenchNumberToWords.convert(90));
System.out.println("*** " + FrenchNumberToWords.convert(91));
System.out.println("*** " + FrenchNumberToWords.convert(97));
System.out.println("*** " + FrenchNumberToWords.convert(100));
System.out.println("*** " + FrenchNumberToWords.convert(101));
System.out.println("*** " + FrenchNumberToWords.convert(110));
System.out.println("*** " + FrenchNumberToWords.convert(120));
System.out.println("*** " + FrenchNumberToWords.convert(200));
System.out.println("*** " + FrenchNumberToWords.convert(201));
System.out.println("*** " + FrenchNumberToWords.convert(232));
System.out.println("*** " + FrenchNumberToWords.convert(999));
System.out.println("*** " + FrenchNumberToWords.convert(1000));
System.out.println("*** " + FrenchNumberToWords.convert(1001));
System.out.println("*** " + FrenchNumberToWords.convert(10000));
System.out.println("*** " + FrenchNumberToWords.convert(10001));
System.out.println("*** " + FrenchNumberToWords.convert(100000));
System.out.println("*** " + FrenchNumberToWords.convert(2000000));
System.out.println("*** " + FrenchNumberToWords.convert(3000000000L));
System.out.println("*** " + FrenchNumberToWords.convert(2147483647));
/*
*** OUTPUT
*** zéro
*** neuf
*** dix-neuf
*** vingt et un
*** vingt-huit
*** soixante et onze
*** soixante-douze
*** quatre-vingt
*** quatre-vingt-un
*** quatre-vingt-neuf
*** quatre-vingt-dix
*** quatre-vingt-onze
*** quatre-vingt-dix-sept
*** cent
*** cent un
*** cent dix
*** cent vingt
*** deux cents
*** deux cent un
*** deux cent trente-deux
*** neuf cent quatre-vingt-dix-neuf
*** mille
*** mille un
*** dix mille
*** dix mille un
*** cent mille
*** deux millions
*** trois milliards
*** deux milliards cent quarante-sept millions
** quatre cent quatre-vingt-trois mille six cent quarante-sept
*/
}
}

You can handle "dollar and cent" conversion by calling the "convert" method two times.

String phrase = "12345.67" ;
Float num = new Float( phrase ) ;
int dollars = (int)Math.floor( num ) ;
int cent = (int)Math.floor( ( num - dollars ) * 100.0f ) ;

String s = "$ " + EnglishNumberToWords.convert( dollars ) + " and "
+ EnglishNumberToWords.convert( cent ) + " cents" ;

Another way to use a built-in function of your DBMS (if available).
For Oracle

SQL> select to_char(to_date(873,'J'), 'JSP') as converted_form from dual;

CONVERTED_FORM
---------------------------
EIGHT HUNDRED SEVENTY-THREE

SQL>
'JSP' means :
J : the Julian format.
SP : spells the word for the number passed to to_date

NUMBERS TO WORDS using html and javascript

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<br><br>
<h1>NUMBER TO WORDS</h1>
<input type="5ber" id="nm1" placeholder="number"><br><br>
<input type="button" value="To Words" id="optin" onclick="pldstrng()">
<p id="value" style="color: blueviolet;"></p>

<script>
function pldstrng() {
var k = document.getElementById("nm1").value
var a = String(k)
for (let i = 0; i < a.length; i++) {
switch (a[i]) {
case "1":
document.getElementById("value").innerHTML+="one"+" "
break
case "2":
document.getElementById("value").innerHTML+="two"+" "
break
case "3":
document.getElementById("value").innerHTML+="three"+" "
break
case "4":
document.getElementById("value").innerHTML+="four"+" "
break
case "5":
document.getElementById("value").innerHTML+="five"+" "
break
case "6":
document.getElementById("value").innerHTML+="six"+" "
break
case "7":
document.getElementById("value").innerHTML+="siven"+" "
break
case "8":
document.getElementById("value").innerHTML+="eight"+" "
break
case "9":
document.getElementById("value").innerHTML+="nine"+" "
break
case "0":
document.getElementById("value").innerHTML+="zero"+" "
break
default :
document.getElementById("value").innerHTML="enter a number / digit"+" "
}
}
}
</script>
</body>

</html>

JavaScript numbers to Words

JavaScript is parsing the group of 3 numbers as an octal number when there's a leading zero digit. When the group of three digits is all zeros, the result is the same whether the base is octal or decimal.

But when you give JavaScript '009' (or '008'), that's an invalid octal number, so you get zero back.

If you had gone through the whole set of numbers from 190,000,001 to 190,000,010 you'd hav seen JavaScript skip '...,008' and '...,009' but emit 'eight' for '...,010'. That's the 'Eureka!' moment.

Change:

for (j = 0; j < finlOutPut.length; j++) {
finlOutPut[j] = triConvert(parseInt(finlOutPut[j]));
}

to

for (j = 0; j < finlOutPut.length; j++) {
finlOutPut[j] = triConvert(parseInt(finlOutPut[j],10));
}

Code also kept on adding commas after every non-zero group, so I played with it and found the right spot to add the comma.

Old:

for (b = finlOutPut.length - 1; b >= 0; b--) {
if (finlOutPut[b] != "dontAddBigSufix") {
finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr] + ' , ';
bigScalCntr++;
}
else {
//replace the string at finlOP[b] from "dontAddBigSufix" to empty String.
finlOutPut[b] = ' ';
bigScalCntr++; //advance the counter
}
}

//convert The output Arry to , more printable string
for(n = 0; n<finlOutPut.length; n++){
output +=finlOutPut[n];
}

New:

for (b = finlOutPut.length - 1; b >= 0; b--) {
if (finlOutPut[b] != "dontAddBigSufix") {
finlOutPut[b] = finlOutPut[b] + bigNumArry[bigScalCntr]; // <<<
bigScalCntr++;
}
else {
//replace the string at finlOP[b] from "dontAddBigSufix" to empty String.
finlOutPut[b] = ' ';
bigScalCntr++; //advance the counter
}
}

//convert The output Arry to , more printable string
var nonzero = false; // <<<
for(n = 0; n<finlOutPut.length; n++){
if (finlOutPut[n] != ' ') { // <<<
if (nonzero) output += ' , '; // <<<
nonzero = true; // <<<
} // <<<
output +=finlOutPut[n];
}

Numbers to words along with decimal value count

Use the same function for getting the whole and the decimal part

function withDecimal(n) {
var nums = n.toString().split('.')
var whole = convertNumberToWords(nums[0])
if (nums.length == 2) {
var fraction = convertNumberToWords(nums[1])
return whole + 'and ' + fraction;
} else {
return whole;
}
}

console.log(withDecimal(51.32)) //Fifty One and Thirty Two
console.log(withDecimal(29.0)) //Twenty Nine

How to convert numbers to words without using num2word library?

Your first statement logic is incorrect. Unless Number is 1 or smaller, that statement is always True; 200 is greater than 1 as well.

Use and instead, and include 1 in the acceptable values:

if (Number >= 1) and (Number <= 19):

You could use chaining as well:

if 1 <= Number <= 19:

For numbers of 20 or larger, use divmod() to get both the number of tens and the remainder:

tens, remainder = divmod(Number, 10)

Demo:

>>> divmod(42, 10)
(4, 2)

then use those values to build your number from the parts:

return num2words2[tens - 2] + '-' + num2words1[below_ten]

Don't forget to account for cases when the number is above 20 and doesn't have a remainder from the divmod operation:

return num2words2[tens - 2] + '-' + num2words1[remainder] if remainder else num2words2[tens - 2]

All put together:

def number(Number):
if 0 <= Number <= 19:
return num2words1[Number]
elif 20 <= Number <= 99:
tens, remainder = divmod(Number, 10)
return num2words2[tens - 2] + '-' + num2words1[remainder] if remainder else num2words2[tens - 2]
else:
print('Number out of implemented range of numbers.')

How to display cash value in words using jQuery?

Working Demo

<form name="test"><input type="text" size="18" value="" name="inum"><br><br><input type="button" onclick="test.rnum.value = toWords(test.inum.value);" value="To Words"><br><br><textarea rows="5" cols="40" name="rnum"></textarea></form><body>

<script type="text/javascript">
// American Numbering System
var th = ['', 'thousand', 'million', 'billion', 'trillion'];

var dg = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];

var tn = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];

var tw = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];

function toWords(s) {
s = s.toString();
s = s.replace(/[\, ]/g, '');
if (s != parseFloat(s)) return 'not a number';
var x = s.indexOf('.');
if (x == -1) x = s.length;
if (x > 15) return 'too big';
var n = s.split('');
var str = '';
var sk = 0;
for (var i = 0; i < x; i++) {
if ((x - i) % 3 == 2) {
if (n[i] == '1') {
str += tn[Number(n[i + 1])] + ' ';
i++;
sk = 1;
} else if (n[i] != 0) {
str += tw[n[i] - 2] + ' ';
sk = 1;
}
} else if (n[i] != 0) {
str += dg[n[i]] + ' ';
if ((x - i) % 3 == 0) str += 'hundred ';
sk = 1;
}
if ((x - i) % 3 == 1) {
if (sk) str += th[(x - i - 1) / 3] + ' ';
sk = 0;
}
}
if (x != s.length) {
var y = s.length;
str += 'point ';
for (var i = x + 1; i < y; i++) str += dg[n[i]] + ' ';
}
return str.replace(/\s+/g, ' ');

}
</script>

Display Amount in Words in vb.net

Try this

Function NumberToText(ByVal n As Integer) As String

Select Case n
Case 0
Return ""

Case 1 To 19
Dim arr() As String = {"One","Two","Three","Four","Five","Six","Seven", _
"Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fourteen", _
"Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"}
Return arr(n-1) & " "

Case 20 to 99
Dim arr() as String = {"Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"}
Return arr(n\10 -2) & " " & NumberToText(n Mod 10)

Case 100 to 199
Return "One Hundred " & NumberToText(n Mod 100)

Case 200 to 999
Return NumberToText(n\100) & "Hundreds " & NumberToText(n mod 100)

Case 1000 to 1999
Return "One Thousand " & NumberToText(n Mod 1000)

Case 2000 to 999999
Return NumberToText(n\1000) & "Thousands " & NumberToText(n Mod 1000)

Case 1000000 to 1999999
Return "One Million " & NumberToText(n Mod 1000000)

Case 1000000 to 999999999
Return NumberToText(n\1000000) & "Millions " & NumberToText(n Mod 1000000)

Case 1000000000 to 1999999999
Return "One Billion " & NumberTotext(n Mod 1000000000)

Case Else
Return NumberToText(n\1000000000) & "Billion " _
& NumberToText(n mod 1000000000)
End Select
End Function


Related Topics



Leave a reply



Submit