Convert number to words - first, second, third and so on
This seems like a simpler approach, a nice recursive algorithm
CREATE FUNCTION fnIntegerToWords(@Number as BIGINT)
RETURNS VARCHAR(1024)
AS
BEGIN
DECLARE @Below20 TABLE (ID int identity(0,1), Word varchar(32))
DECLARE @Below100 TABLE (ID int identity(2,1), Word varchar(32))
INSERT @Below20 (Word) VALUES
( 'Zero'), ('One'),( 'Two' ), ( 'Three'),
( 'Four' ), ( 'Five' ), ( 'Six' ), ( 'Seven' ),
( 'Eight'), ( 'Nine'), ( 'Ten'), ( 'Eleven' ),
( 'Twelve' ), ( 'Thirteen' ), ( 'Fourteen'),
( 'Fifteen' ), ('Sixteen' ), ( 'Seventeen'),
('Eighteen' ), ( 'Nineteen' )
INSERT @Below100 VALUES ('Twenty'), ('Thirty'),('Forty'), ('Fifty'),
('Sixty'), ('Seventy'), ('Eighty'), ('Ninety')
declare @belowHundred as varchar(126)
if @Number > 99 begin
select @belowHundred = dbo.fnIntegerToWords( @Number % 100)
end
DECLARE @English varchar(1024) =
(
SELECT Case
WHEN @Number = 0 THEN ''
WHEN @Number BETWEEN 1 AND 19
THEN (SELECT Word FROM @Below20 WHERE ID=@Number)
WHEN @Number BETWEEN 20 AND 99
THEN (SELECT Word FROM @Below100 WHERE ID=@Number/10)+ '-' +
dbo.fnIntegerToWords( @Number % 10)
WHEN @Number BETWEEN 100 AND 999
THEN (dbo.fnIntegerToWords( @Number / 100)) +' Hundred '+
Case WHEN @belowHundred <> '' THEN 'and ' + @belowHundred else @belowHundred end
WHEN @Number BETWEEN 1000 AND 999999
THEN (dbo.fnIntegerToWords( @Number / 1000))+' Thousand '+
dbo.fnIntegerToWords( @Number % 1000)
WHEN @Number BETWEEN 1000000 AND 999999999
THEN (dbo.fnIntegerToWords( @Number / 1000000))+' Million '+
dbo.fnIntegerToWords( @Number % 1000000)
WHEN @Number BETWEEN 1000000000 AND 999999999999
THEN (dbo.fnIntegerToWords( @Number / 1000000000))+' Billion '+
dbo.fnIntegerToWords( @Number % 1000000000)
ELSE ' INVALID INPUT' END
)
SELECT @English = RTRIM(@English)
SELECT @English = RTRIM(LEFT(@English,len(@English)-1))
WHERE RIGHT(@English,1)='-'
RETURN (@English)
END
.NET or 3rd party library to convert number into first , second , third , etc...
Humanizer is a great library for this: Humanizr
Install-Package Humanizer
With Humanizer, you'd do:
int number = 5;
string ordinal = number.ToOrdinalWords()
PHP: increment counter function using words (i.e. First, Second, Third, etc.. )
There is a class from PEAR package can do that:
<?php
// include class
include("Numbers/Words.php");
// create object
$nw = new Numbers_Words();
// convert to string
echo "600 in words is " . $nw->toWords(600);
?>
Source.
How to convert number into word
Can you try this one.
function createFullWordOrdinal($number)
{
$ord1 = array(1 => "first", 2 => "second", 3 => "third", 5 => "fifth", 8 => "eight", 9 => "ninth", 11 => "eleventh", 12 => "twelfth", 13 => "thirteenth", 14 => "fourteenth", 15 => "fifteenth", 16 => "sixteenth", 17 => "seventeenth", 18 => "eighteenth", 19 => "nineteenth");
$num1 = array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen");
$num10 = array("zero", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety");
$places = array(2 => "hundred", 3 => "thousand", 6 => "million", 9 => "billion", 12 => "trillion", 15 => "quadrillion", 18 => "quintillion", 21 => "sextillion", 24 => "septillion", 27 => "octillion");
$number = array_reverse(str_split($number));
if ($number[0] == 0)
{
if ($number[1] >= 2)
$out = str_replace("y", "ieth", $num10[$number[1]]);
else
$out = $num10[$number[1]]."th";
}
else if (isset($number[1]) && $number[1] == 1)
{
$out = $ord1[$number[1] . $number[0]];
}
else
{
if (array_key_exists($number[0], $ord1))
$out = $ord1[$number[0]];
else
$out = $num1[$number[0]]."th";
}
if((isset($number[0]) && $number[0] == 0) || (isset($number[1]) && $number[1] == 1))
{
$i = 2;
}
else
{
$i = 1;
}
while ($i < count($number))
{
if ($i == 1)
{
$out = $num10[$number[$i]] . " " . $out;
$i++;
}
else if ($i == 2)
{
$out = $num1[$number[$i]] . " hundred " . $out;
$i++;
}
else
{
if (isset($number[$i + 2]))
{
$tmp = $num1[$number[$i + 2]] . " hundred ";
$tmpnum = $number[$i + 1].$number[$i];
if ($tmpnum < 20)
$tmp .= $num1[$tmpnum] . " " . $places[$i] . " ";
else
$tmp .= $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " ";
$out = $tmp . $out;
$i+=3;
}
else if (isset($number[$i + 1]))
{
$tmpnum = $number[$i + 1].$number[$i];
if ($tmpnum < 20)
$out = $num1[$tmpnum] . " " . $places[$i] . " " . $out;
else
$out = $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " " . $out;
$i+=2;
}
else
{
$out = $num1[$number[$i]] . " " . $places[$i] . " " . $out;
$i++;
}
}
}
return $out;
}
This will give you the output you want.
createFullWordOrdinal(1) ----> first
createFullWordOrdinal(2) ----> second
createFullWordOrdinal(3) ----> third
createFullWordOrdinal(4) ----> fourth
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 replace 1 with first, 2 with second,3 with third etc
There is no inbuilt function for it.
I did write one for up to 99:
var special = ['zeroth','first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth', 'thirteenth', 'fourteenth', 'fifteenth', 'sixteenth', 'seventeenth', 'eighteenth', 'nineteenth'];
var deca = ['twent', 'thirt', 'fort', 'fift', 'sixt', 'sevent', 'eight', 'ninet'];
function stringifyNumber(n) {
if (n < 20) return special[n];
if (n%10 === 0) return deca[Math.floor(n/10)-2] + 'ieth';
return deca[Math.floor(n/10)-2] + 'y-' + special[n%10];
}
// TEST LOOP SHOWING RESULTS
for (var i=0; i<100; i++) console.log(stringifyNumber(i));
DEMO: http://jsbin.com/AqetiNOt/1/edit
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];
}
Is there a library to convert integer to first/second/third
The python inflect
package has a method for converting numerals into ordinals:
import inflect
p = inflect.engine()
for i in range(1,25,5):
print(p.ordinal(i))
displays:
1st
6th
11th
16th
21st
Related Topics
Creating a Composite Foreign Key in SQL Server 2008
SQL Unpivot Multiple Columns Data
Possible to Do a Delete with a Having Clause
Postgresql Does Not Use a Partial Index
Split String into Table Given Row Delimiter and Column Delimiter in SQL Server
SQL Constraint: Date a Is Before Date B -- How
MySQL Question About Scheduling
Calling Stored Procedure While Passing Parameters from Access Module in Vba
Why Historically Do People Use 255 Not 256 for Database Field Magnitudes
How to Find the Number of Occurrences of a Particular Character in a String Using SQL
How to Find the .Net Framework Version Used in an Ssis 2008 R2 Package
Is SQL Order by Clause Guaranteed to Be Stable ( by Standards)