Ranjan Sakalley

Sponsors

The Lounge

News

  • CodeBetter.Com Home



    I'm test-driven!



Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Convert numbers to text

 A new problem for the day.

program input :
1,291

program output:

"one thousand two hundred and ninety one"

Basically pick out the number in the form 1 x 10^3 + 2 x 10^2 + 9 x 10 + 1,
have this as a list, and switch each element with the text required.
Remember that the tenth place should have a different set (twenty,thrity etc) and that 11 to 19 are another list again.
Sometimes you just need to hard-code

p.s.: I will post the code as a comment.


Posted 01-05-2005 11:04 PM by rsakalley

[Advertisement]

Comments

Ranjan wrote re: Convert numbers to text
on 01-05-2005 6:07 PM
Here is the solution from one of our developers, (vb)

Namespace Proteans.Utils.Conversion
Public Class ConvertMoney
Private m_19AndUnder(19) As String
Private m_Tens(9) As String
Private m_Hundred As String
Private m_Groups(10) As String
Private m_Dollar As String
Private m_Dollars As String
Private m_NoCents As String
Private m_Cent As String
Private m_Cents As String
Private m_Hyphen As String
Private m_And As String
Private m_InvalidAmount As String

Public Sub New()
'Initialize all the "words"
m_19AndUnder(0) = "Zero"
m_19AndUnder(1) = "One"
m_19AndUnder(2) = "Two"
m_19AndUnder(3) = "Three"
m_19AndUnder(4) = "Four"
m_19AndUnder(5) = "Five"
m_19AndUnder(6) = "Six"
m_19AndUnder(7) = "Seven"
m_19AndUnder(8) = "Eight"
m_19AndUnder(9) = "Nine"
m_19AndUnder(10) = "Ten"
m_19AndUnder(11) = "Eleven"
m_19AndUnder(12) = "Twelve"
m_19AndUnder(13) = "Thirteen"
m_19AndUnder(14) = "Fourteen"
m_19AndUnder(15) = "Fifteen"
m_19AndUnder(16) = "Sixteen"
m_19AndUnder(17) = "Seventeen"
m_19AndUnder(18) = "Eighteen"
m_19AndUnder(19) = "Nineteen"

m_Tens(2) = "Twenty"
m_Tens(3) = "Thirty"
m_Tens(4) = "Forty"
m_Tens(5) = "Fifty"
m_Tens(6) = "Sixty"
m_Tens(7) = "Seventy"
m_Tens(8) = "Eighty"
m_Tens(9) = "Ninety"

m_Hundred = "Hundred"

m_Groups(1) = ""
m_Groups(2) = "Thousand"
m_Groups(3) = "Million"
m_Groups(4) = "Billion"
m_Groups(5) = "Trillion"
m_Groups(6) = "Quadrillion"
m_Groups(7) = "Quintillion"
m_Groups(8) = "Sextillion"
m_Groups(9) = "Septillion"
m_Groups(10) = "Octillion"

m_Dollar = " Dollar"
m_Dollars = " Dollars"

m_NoCents = "No Cents"
'm_Cent & m_Cents could both be changed to "/100"
m_Cent = " Cent"
m_Cents = " Cents"

'Used for #s like: 23 = "Twenty-Three"
m_Hyphen = "-"

'Used between dollars & cents: "One Dollar and 12 Cents"
m_And = " and "

m_InvalidAmount = "Invalid Dollar Amount."
End Sub

Public Function MonetaryToWords(ByVal Value As Object) As String
Dim decValue As Object
Dim sValue As String
Dim iDecimal As Integer
Dim sCents As String
Dim sDollars As String

'Convert input into a Decimal value (up to 28 digits)
decValue = CDec(Value)
If decValue < 0 Then GoTo ER

'Convert the Decimal value back into a string. This eliminates
' any format characters such as "$" or ",".
sValue = CStr(decValue)

'Find the decimal point & extract the dollars from the cents
iDecimal = InStr(1, sValue, ".")
If iDecimal = 0 Then
sDollars = sValue
sCents = "00"
Else
'Extract decimal value
sCents = Mid$(sValue, iDecimal + 1)
If Len(sCents) > 2 Then GoTo ER

'Extract dollars
sDollars = Left$(sValue, iDecimal - 1)

'Fill-out decimal places to two digits
sCents = Left$(sCents & "00", 2)
End If

'At this point,
' sDollars = the whole dollar value (0.. approx 79 Octillion)
' sCents = the cents (00..99)

Debug.Assert(Len(sCents) = 2)
Debug.Assert(Len(sDollars) > 0)
Debug.Assert(Len(sDollars) < 31)

Select Case sCents
Case "00"
sCents = m_NoCents

Case "01"
sCents = sCents & m_Cent

Case Else
sCents = sCents & m_Cents
End Select

MonetaryToWords = DollarsToWords(sDollars) & m_And & sCents

Exit Function

ER:

MonetaryToWords = m_InvalidAmount
End Function

Private Function DollarsToWords(ByVal sDollars As String) As String
Dim sWords As String
Dim decValue As Object
Dim sRemaining As String
Dim s3Digits As String
Dim iGroup As Integer
Dim i100s As Integer
Dim i10s As Integer
Dim i1s As Integer
Dim i99OrLess As Integer
Dim sWork As String

'We had better be passing a valid number
Debug.Assert(IsNumeric(sDollars))

'Check for special cases. This also serves to validate the value
decValue = CDec(sDollars)
Select Case decValue
Case 0
DollarsToWords = m_19AndUnder(decValue) & m_Dollars
Exit Function

Case 1
DollarsToWords = m_19AndUnder(decValue) & m_Dollar
Exit Function

End Select

'There should be no insignificant zeroes, "punctuation" or decimals
Debug.Assert(sDollars = CStr(decValue))

iGroup = 0
sRemaining = sDollars
sWords = ""

'Extract each group of three digits, convert to words and prefix to result
While Len(sRemaining) > 0
iGroup = iGroup + 1

'Extract next group of three digits
If Len(sRemaining) > 3 Then
s3Digits = Right$(sRemaining, 3)
sRemaining = Left$(sRemaining, Len(sRemaining) - 3)
Else
'Fill-out group to three digits
s3Digits = Right$("00" & sRemaining, 3)
sRemaining = ""
End If

Debug.Assert(Len(s3Digits) = 3)

If s3Digits <> "000" Then
i100s = CInt(Left$(s3Digits, 1))
i10s = CInt(Mid$(s3Digits, 2, 1))
i1s = CInt(Right$(s3Digits, 1))
i99OrLess = (i10s * 10) + i1s
sWork = " " & m_Groups(iGroup)

Select Case True
'Do we have 20..99?
Case i10s > 1
Debug.Assert(i10s <= 9)

If i1s > 0 Then
Debug.Assert(i1s <= 9)

sWork = m_Tens(i10s) & m_Hyphen & m_19AndUnder(i1s) & sWork
Else
sWork = m_Tens(i10s) & sWork
End If

'Do we have 01..19?
Case i99OrLess > 0
Debug.Assert(i99OrLess <= 99)

sWork = m_19AndUnder(i99OrLess) & sWork

Case Else
'If we're here, it's because there are no tens or ones
Debug.Assert(i99OrLess = 0)
Debug.Assert(i10s = 0)
Debug.Assert(i1s = 0)
Debug.Assert(Right$(s3Digits, 2) = "00")

'If there's no tens or ones, there better be hundreds
Debug.Assert(i100s > 0)

End Select

If i100s > 0 Then
Debug.Assert(i100s <= 9)

sWork = m_19AndUnder(i100s) & " " & m_Hundred & " " & sWork
End If

Debug.Assert(Len(Trim$(sWork)) > 0)

sWords = sWork & " " & sWords
End If
End While

DollarsToWords = Trim$(sWords) & m_Dollars
End Function


End Class
Mihir Solanki wrote re: Convert numbers to text
on 01-05-2005 7:04 PM
Well, there must be a simple solution of it.Try Regular Expression ...
Ranjan wrote re: Convert numbers to text
on 01-05-2005 7:20 PM
As i mentioned, you need to hardcode. Its basically because its english, and not any other scientific language like sanksrit, where you could basically have a formula(regex) working.
Raymond Chen wrote re: Convert numbers to text
on 01-05-2005 7:30 PM
Note however that the code doesn't quite follow the rules you set out in the original challenge: It leaves out the "and" in "one thousand two hundred and ninety one".
Ranjan wrote re: Convert numbers to text
on 01-05-2005 7:48 PM
Thanks for testing it :)

I know the "and" thingy, thanks for mentioning it, the code's just a starter on one of the ways you could do it, and i actually wanted to find a better way.
Stoyan wrote re: Convert numbers to text
on 02-15-2005 5:35 AM
Some comments:

a) I guess that the m_xxx members should be static (shared) or constants, not instance variables.

b) why are these "ByVal Value As Object", "Dim decValue As Object", etc.? Make it explicit, e.g. Public Function MonetaryToWords(ByVal Value As Decimal) As String

c) iDecimal = InStr(1, sValue, ".") won't work for certain cultures, e.g. in Bulgaria (where I'm from) the decimal point is actually a comma :) which leads to Public Function MonetaryToWords(ByVal Value As Decimal, ByVal formatProvider as IFormatProvider) As String (or something like this)

d) sCents = Left$(sCents & "00", 2), as well as Instr, etc. show that this code was copied and pasted from an old VB (non .NET) source, there are PadLeft, IndexOf, etc. methods on the String class, etc.

e) the m_InvalidAmount might seem like a good return value for the function developer, but I'd rather have a function returning bool and have a out parameter for the result string and return true and false (or a tuple of these).

(I got up with my ass up this morning ;)
Ranjan wrote re: Convert numbers to text
on 02-15-2005 4:35 PM
Stoyan,
Thanks for the review.
The code was converted from VB to VB.Net, as you guessed. Its just a way to do this. Still havent found a better algorithm (though a better code design has been implemented in C#).
About internationalization (or localization, as you may see it), yes, we do not have focus on that part (i guess you just need to write a wrap and use resource strings insted).
And yes, I prefer the function returning whats been asked, and the inputs are invalid, an exception. The developer has already learnt his lesson.


sas wrote re: Convert numbers to text using vc++
on 04-01-2005 1:48 AM
Hi,

i want the code for convert number to text using vc++.....
Dean wrote re: Convert numbers to text
on 06-24-2005 10:35 AM
Here is a solution written in VB 6:

' ==============================================================
' SubRoutine: convertToText
' Purpose: Converts a number into its text equivalent.
' ==============================================================
Public Function convertToText(ByVal numbers As String) As String
Dim text As String, bits(90) As String
Dim sAnd(1) As String, hundred(1) As String
Dim flag As Integer, groups() As Integer
Dim firstDigit As Integer, secondDigit As Integer, thirdDigit As Integer
Dim H As Integer, A As Integer, D As Integer, x As Integer
Dim minus As String

On Error GoTo ErrorHandler

numbers = Replace(numbers, ",", "")

Dim divisor As Variant
divisor = Array("Trillion ", "Billion ", "Million ", "Thousand ", "")
ReDim groups(UBound(divisor))

sAnd(0) = "": sAnd(1) = "and "
hundred(0) = "": hundred(1) = "Hundred "

If Mid$(numbers, 1, 1) = "-" Then
minus = "minus "
numbers = Mid$(numbers, 2)
End If

Do Until Mid$(numbers, 1, 1) <> "0"
numbers = Mid$(numbers, 2)
Loop

If numbers = "" Then
convertToText = "zero"
Exit Function
End If

Dim i As Integer
For i = 1 To Len(numbers)
If Not Mid$(numbers, i, 1) Like "[0-9]" Then
convertToText = "invalid data"
Exit Function
End If
Next

If Len(numbers) > (UBound(divisor) + 1) * 3 Then
convertToText = "outside of number range"
Exit Function
End If

bits(0) = "": bits(1) = "One "
bits(2) = "Two ": bits(3) = "Three "
bits(4) = "Four ": bits(5) = "Five "
bits(6) = "Six ": bits(7) = "Seven "
bits(8) = "Eight ": bits(9) = "Nine "
bits(10) = "Ten ": bits(11) = "Eleven "
bits(12) = "Twelve ": bits(13) = "Thirteen "
bits(14) = "Fourteen ": bits(15) = "Fifteen "
bits(16) = "Sixteen ": bits(17) = "Seventeen "
bits(18) = "Eighteen ": bits(19) = "Nineteen "
bits(20) = "Twenty ": bits(30) = "Thirty "
bits(40) = "Forty ": bits(50) = "Fifty "
bits(60) = "Sixty ": bits(70) = "Seventy "
bits(80) = "Eighty ": bits(90) = "Ninety "

x = UBound(divisor)
Do Until Len(numbers) = 0
groups(x) = CInt(Right(numbers, 3))
If Len(numbers) <= 3 Then
numbers = ""
Else: numbers = Mid$(numbers, 1, Len(numbers) - 3)
End If
x = x - 1
Loop

For i = x + 1 To UBound(divisor) 'convert in groups - 111,222,333,444,555
firstDigit = groups(i) \ 100
secondDigit = (groups(i) - (groups(i) \ 100) * 100) \ 10
thirdDigit = groups(i) - (firstDigit * 100) - (secondDigit * 10)

If secondDigit = 1 Then
secondDigit = secondDigit + 9 + thirdDigit
thirdDigit = 0
Else: secondDigit = secondDigit * 10
End If

If i < UBound(divisor) And (firstDigit + secondDigit + thirdDigit) > 0 Then
flag = 1 'setting flag for "and" in last group
End If

If firstDigit > 0 Then
H = 1
Else
H = 0
End If

If (firstDigit > 0 Or (i = UBound(divisor) And flag = 1)) And (secondDigit > 0 Or thirdDigit > 0) Then
A = 1
Else
A = 0
End If

If (firstDigit + secondDigit + thirdDigit) > 0 Then
D = i
Else
D = UBound(divisor)
End If

text = text & bits(firstDigit) & hundred(H) & sAnd(A) & bits(secondDigit) & bits(thirdDigit) & divisor(D)
Next

convertToText = minus & text
Exit Function

ErrorHandler:

End Function
Dean wrote re: Convert numbers to text
on 06-24-2005 11:36 AM
...and here is the equivalent in VB.Net:


' ==============================================================
' SubRoutine: convertToText
' Purpose: Converts a number into its text equivalent.
' ==============================================================
Public Function convertToText(ByVal numbers As String) As String
Dim text As String
Dim bits(90) As String
Dim sAnd(1) As String
Dim hundred(1) As String
Dim flag As Short
Dim groups() As Short
Dim secondDigit, firstDigit, thirdDigit As Short
Dim D, H, A, x As Short
Dim minus As String

Replace(numbers, ",", "")

Dim divisor() As String = {"Trillion ", "Billion ", "Million ", "Thousand ", ""}
ReDim groups(UBound(divisor))

sAnd(0) = "" : sAnd(1) = "and "
hundred(0) = "" : hundred(1) = "Hundred "

If Mid(numbers, 1, 1) = "-" Then
minus = "minus "
numbers = Mid(numbers, 2)
End If

Do Until Mid(numbers, 1, 1) <> "0"
numbers = Mid(numbers, 2)
Loop

If numbers = "" Then
convertToText = "zero"
Exit Function
End If

Dim i As Short
For i = 1 To Len(numbers)
If Not Mid(numbers, i, 1) Like "[0-9]" Then
convertToText = "invalid data"
Exit Function
End If
Next

If Len(numbers) > (UBound(divisor) + 1) * 3 Then
convertToText = "outside of number range"
Exit Function
End If

bits(0) = "" : bits(1) = "One "
bits(2) = "Two " : bits(3) = "Three "
bits(4) = "Four " : bits(5) = "Five "
bits(6) = "Six " : bits(7) = "Seven "
bits(8) = "Eight " : bits(9) = "Nine "
bits(10) = "Ten " : bits(11) = "Eleven "
bits(12) = "Twelve " : bits(13) = "Thirteen "
bits(14) = "Fourteen " : bits(15) = "Fifteen "
bits(16) = "Sixteen " : bits(17) = "Seventeen "
bits(18) = "Eighteen " : bits(19) = "Nineteen "
bits(20) = "Twenty " : bits(30) = "Thirty "
bits(40) = "Forty " : bits(50) = "Fifty "
bits(60) = "Sixty " : bits(70) = "Seventy "
bits(80) = "Eighty " : bits(90) = "Ninety "

x = UBound(divisor)
Do Until Len(numbers) = 0
groups(x) = CShort(Right(numbers, 3))
If Len(numbers) <= 3 Then
numbers = ""
Else : numbers = Mid(numbers, 1, Len(numbers) - 3)
End If
x = x - 1
Loop

For i = x + 1 To UBound(divisor) 'convert in groups - 111,222,333,444,555
firstDigit = groups(i) \ 100
secondDigit = (groups(i) - (groups(i) \ 100) * 100) \ 10
thirdDigit = groups(i) - (firstDigit * 100) - (secondDigit * 10)

If secondDigit = 1 Then
secondDigit = secondDigit + 9 + thirdDigit
thirdDigit = 0
Else : secondDigit = secondDigit * 10
End If

If i < UBound(divisor) And (firstDigit + secondDigit + thirdDigit) > 0 Then
flag = 1 'setting flag for "and" in last group
End If

If firstDigit > 0 Then
H = 1
Else
H = 0
End If

If (firstDigit > 0 Or (i = UBound(divisor) And flag = 1)) And (secondDigit > 0 Or thirdDigit > 0) Then
A = 1
Else
A = 0
End If

If (firstDigit + secondDigit + thirdDigit) > 0 Then
D = i
Else
D = UBound(divisor)
End If

text = text & bits(firstDigit) & hundred(H) & sAnd(A) & bits(secondDigit) & bits(thirdDigit) & divisor(D)
Next

convertToText = minus & text

End Function
Dean wrote re: Convert numbers to text
on 06-24-2005 11:42 AM
Whoops! I made a mistake there on that .Net version. The first line of code should actually read:


numbers = Replace(numbers, ",", "")



Wahoodlum wrote re: Convert numbers to text
on 12-12-2005 4:52 PM
#pragma once


namespace SoficomCollectionSystem
{
public __gc class Conversion
{
private:
int SearchStr(String *sStr, String *cStr)
{
return sStr->IndexOf(cStr);
}
String* Left(String* numStr, int index)
{
String* retStr = numStr->Substring(0,index);
return retStr;
}
String* Right(String* numStr, int index)
{
int Length = numStr->Length;
if(Length < index) index = Length;
String* retStr = numStr->Substring(numStr->Length-index);
return retStr;
}
String* Mid(String* numStr, int Start, int Length)
{
String* retStr = numStr->Substring(Start, Length);
return retStr;
}

System::Boolean IsValidNumber(String *cvNumber)
{
int DecimalDigits, MaxLength = 15;
DecimalDigits = SearchStr(cvNumber,".");
if(DecimalDigits!=-1 && cvNumber->Length < 19)
MaxLength = (cvNumber->Length-15)+15;
try{
if(cvNumber->Length == 0 || cvNumber->Length > MaxLength){
MessageBox::Show(S"Please enter a valid number.");
return false;
}
else{
System::Globalization::NumberFormatInfo* nfi = new System::Globalization::NumberFormatInfo;
cvNumber->ToDouble(nfi);
return true;
}
}
catch(...){
MessageBox::Show(S"Please enter a valid number.");
return false;
}
}

String* ConvertHundreds(String* strNum)
{
String* Result;
if(System::Convert::ToDecimal(strNum)==0) return S"";
strNum = Right(String::Concat("000",strNum),3);
if(String::Compare(Left(strNum,1),"0")!=0){
Result = String::Concat(ConvertDigit(Left(strNum,1))," Hundred ");
}
if(String::Compare(Mid(strNum,1,1),"0")!=0){
Result = String::Concat(Result,ConvertTens(Mid(strNum,1,2)));
}
else{
Result = String::Concat(Result,ConvertDigit(Mid(strNum,2,1)));
}
return Result;
}

String* ConvertCent(String* strNum)
{
String* Result;
if(System::Convert::ToDecimal(strNum)==0) return S"";
strNum = Right(String::Concat("000",strNum),3);
if(String::Compare(Left(strNum,1),"0")!=0){
Result = String::Concat(ConvertChiffre(Left(strNum,1))," Cent ");
}
if(String::Compare(Mid(strNum,1,1),"0")!=0){
Result = String::Concat(Result,ConvertDix(Mid(strNum,1,2)));
}
else{
Result = String::Concat(Result,ConvertChiffre(Mid(strNum,2,1)));
}
return Result;
}

String* ConvertTens(String* strTens)
{
String* sTens;
if(System::Convert::ToInt16(Left(strTens,1))==1){
int nTens = System::Convert::ToInt16(strTens);
switch(nTens){
case 10: {sTens = String::Copy(S"Ten"); break;}
case 11: {sTens = String::Copy(S"Eleven"); break;}
case 12: {sTens = String::Copy(S"Twelve"); break;}
case 13: {sTens = String::Copy(S"Thirteen"); break;}
case 14: {sTens = String::Copy(S"Fourteen"); break;}
case 15: {sTens = String::Copy(S"Fifteen"); break;}
case 16: {sTens = String::Copy(S"Sixteen"); break;}
case 17: {sTens = String::Copy(S"Seventeen"); break;}
case 18: {sTens = String::Copy(S"Eighteen"); break;}
case 19: {sTens = String::Copy(S"Nineteen"); break;}
}
}
else{
switch(System::Convert::ToInt16(Left(strTens,1))){
case 2: {sTens = String::Copy(S"Twenty "); break;}
case 3: {sTens = String::Copy(S"Thirty "); break;}
case 4: {sTens = String::Copy(S"Fourty "); break;}
case 5: {sTens = String::Copy(S"Fifty "); break;}
case 6: {sTens = String::Copy(S"Sixty "); break;}
case 7: {sTens = String::Copy(S"Seventy "); break;}
case 8: {sTens = String::Copy(S"Eighty "); break;}
case 9: {sTens = String::Copy(S"Ninety "); break;}
}
sTens = String::Concat(sTens,ConvertDigit(Right(strTens,1)));
}
return sTens;
}

String* ConvertDix(String* strTens)
{
String* sTens;
if(System::Convert::ToInt16(Left(strTens,1))==1){
int nTens = System::Convert::ToInt16(strTens);
switch(nTens){
case 10: {sTens = String::Copy(S"Dix"); break;}
case 11: {sTens = String::Copy(S"Onze"); break;}
case 12: {sTens = String::Copy(S"Douze"); break;}
case 13: {sTens = String::Copy(S"Treize"); break;}
case 14: {sTens = String::Copy(S"Quatorze"); break;}
case 15: {sTens = String::Copy(S"Quinze"); break;}
case 16: {sTens = String::Copy(S"Seize"); break;}
case 17: {sTens = String::Copy(S"Dix-Sept"); break;}
case 18: {sTens = String::Copy(S"Dix-huit"); break;}
case 19: {sTens = String::Copy(S"Dix-neuf"); break;}
}
}
else{
switch(System::Convert::ToInt16(Left(strTens,1))){
case 2: {sTens = String::Copy(S"Vingt "); break;}
case 3: {sTens = String::Copy(S"Trente "); break;}
case 4: {sTens = String::Copy(S"Quarante "); break;}
case 5: {sTens = String::Copy(S"Cinquante "); break;}
case 6: {sTens = String::Copy(S"Soixante "); break;}
case 7: {sTens = String::Copy(S"Soixante-dix "); break;}
case 8: {sTens = String::Copy(S"Quatre-vingts "); break;}
case 9: {sTens = String::Copy(S"Quatre-vingt-dix "); break;}
}
sTens = String::Concat(sTens,ConvertChiffre(Right(strTens,1)));
}
return sTens;
}

String* ConvertDigit(String* strDigit)
{
String* sDigit;
int nDigit = System::Convert::ToInt16(strDigit);
switch(nDigit){
case 1: {sDigit = String::Copy(S"One"); break;}
case 2: {sDigit = String::Copy(S"Two"); break;}
case 3: {sDigit = String::Copy(S"Three"); break;}
case 4: {sDigit = String::Copy(S"Four"); break;}
case 5: {sDigit = String::Copy(S"Five"); break;}
case 6: {sDigit = String::Copy(S"Six"); break;}
case 7: {sDigit = String::Copy(S"Seven"); break;}
case 8: {sDigit = String::Copy(S"Eight"); break;}
case 9: {sDigit = String::Copy(S"Nine"); break;}
default: {sDigit = String::Copy(S""); break;}
}
return sDigit;
}

String* ConvertChiffre(String* strDigit)
{
String* sDigit;
int nDigit = System::Convert::ToInt16(strDigit);
switch(nDigit){
case 1: {sDigit = String::Copy(S"Un"); break;}
case 2: {sDigit = String::Copy(S"Deux"); break;}
case 3: {sDigit = String::Copy(S"Trois"); break;}
case 4: {sDigit = String::Copy(S"Quatre"); break;}
case 5: {sDigit = String::Copy(S"Cinq"); break;}
case 6: {sDigit = String::Copy(S"Six"); break;}
case 7: {sDigit = String::Copy(S"Sept"); break;}
case 8: {sDigit = String::Copy(S"Huit"); break;}
case 9: {sDigit = String::Copy(S"Neuf"); break;}
default: {sDigit = String::Copy(S""); break;}
}
return sDigit;
}
public:
String* ConvertCurrencyToEnglish(String* MyNumber)
{
String* Temp;
String* Dollars;
String* Cents="";
int Count = 1, DecimalPlace;
MyNumber = MyNumber->Trim();
if(!IsValidNumber(MyNumber)) return S"Given number is wrong!";
if(String::Compare(Left(MyNumber,1),".")==0)
MyNumber = String::Concat("0",MyNumber);
DecimalPlace = SearchStr(MyNumber,".");
if(DecimalPlace > 0 && MyNumber->Length == DecimalPlace+1)
MyNumber = Left(MyNumber,DecimalPlace);
if(DecimalPlace > 0 && MyNumber->Length > DecimalPlace+1){
Temp = Left(String::Concat(Mid(MyNumber,DecimalPlace+1,MyNumber->Length-(DecimalPlace+1)),"00"),2);
Cents = ConvertTens(Temp);
MyNumber = Left(MyNumber,DecimalPlace);
}
String* Place[] ={S"",S"",S" Thousand ",S" Million ",S" Billion ",S" Trillion "};
while(String::Compare(MyNumber,"") != 0){
Temp = ConvertHundreds(Right(MyNumber,3));
if(String::Compare(Temp,"") != 0){
Dollars = String::Concat(String::Concat(Temp,Place[Count]),Dollars);
}
if(MyNumber->Length > 3)
MyNumber = Left(MyNumber,MyNumber->Length-3);
else
MyNumber = "";
Count = Count + 1;
}
if(String::Compare(Dollars,"")==0)
Dollars = "No Dollars";
else if(String::Compare(Dollars,"One")==0)
Dollars = "One Dollar";
else Dollars = String::Concat(Dollars," Dollars");

if(String::Compare(Cents,"")==0)
Cents = " And No Cents";
else if(String::Compare(Cents,"One")==0)
Cents = " And One Cent";
else Cents = String::Concat(" And ",String::Concat(Cents," Cents "));

return String::Concat(Dollars,Cents);
}

String* ConvertCurrencyToFrench(String* MyNumber)
{
String* Temp;
String* Dollars;
String* Cents="";
int Count = 1, DecimalPlace;
MyNumber = MyNumber->Trim();
if(!IsValidNumber(MyNumber)) return S"Given number is wrong!";
if(String::Compare(Left(MyNumber,1),".")==0)
MyNumber = String::Concat("0",MyNumber);
DecimalPlace = SearchStr(MyNumber,".");
if(DecimalPlace > 0 && MyNumber->Length == DecimalPlace+1)
MyNumber = Left(MyNumber,DecimalPlace);
if(DecimalPlace > 0 && MyNumber->Length > DecimalPlace+1){
Temp = Left(String::Concat(Mid(MyNumber,DecimalPlace+1,MyNumber->Length-(DecimalPlace+1)),"00"),2);
Cents = ConvertDix(Temp);
MyNumber = Left(MyNumber,DecimalPlace);
}
String* Place[] ={S"",S"",S" Mille ",S" Million ",S" Milliard ",S" Trillion "};
while(String::Compare(MyNumber,"") != 0){
Temp = ConvertCent(Right(MyNumber,3));
if(String::Compare(Temp,"") != 0){
Dollars = String::Concat(String::Concat(Temp,Place[Count]),Dollars);
}
if(MyNumber->Length > 3)
MyNumber = Left(MyNumber,MyNumber->Length-3);
else
MyNumber = "";
Count = Count + 1;
}
if(String::Compare(Dollars,"")==0)
Dollars = "Non Dollars";
else if(String::Compare(Dollars,"One")==0)
Dollars = "Un Dollar";
else Dollars = String::Concat(Dollars," Dollars");

if(String::Compare(Cents,"")==0)
Cents = " et Non Cents";
else if(String::Compare(Cents,"One")==0)
Cents = " et Un Cent";
else Cents = String::Concat(" et ",String::Concat(Cents," Cents "));

return String::Concat(Dollars,Cents);
}
};
}
Wahoodlum wrote re: Convert numbers to text
on 12-12-2005 4:54 PM
#pragma once


namespace SoficomCollectionSystem
{
public __gc class Conversion
{
private:
int SearchStr(String *sStr, String *cStr)
{
return sStr->IndexOf(cStr);
}
String* Left(String* numStr, int index)
{
String* retStr = numStr->Substring(0,index);
return retStr;
}
String* Right(String* numStr, int index)
{
int Length = numStr->Length;
if(Length < index) index = Length;
String* retStr = numStr->Substring(numStr->Length-index);
return retStr;
}
String* Mid(String* numStr, int Start, int Length)
{
String* retStr = numStr->Substring(Start, Length);
return retStr;
}

System::Boolean IsValidNumber(String *cvNumber)
{
int DecimalDigits, MaxLength = 15;
DecimalDigits = SearchStr(cvNumber,".");
if(DecimalDigits!=-1 && cvNumber->Length < 19)
MaxLength = (cvNumber->Length-15)+15;
try{
if(cvNumber->Length == 0 || cvNumber->Length > MaxLength){
MessageBox::Show(S"Please enter a valid number.");
return false;
}
else{
System::Globalization::NumberFormatInfo* nfi = new System::Globalization::NumberFormatInfo;
cvNumber->ToDouble(nfi);
return true;
}
}
catch(...){
MessageBox::Show(S"Please enter a valid number.");
return false;
}
}

String* ConvertHundreds(String* strNum)
{
String* Result;
if(System::Convert::ToDecimal(strNum)==0) return S"";
strNum = Right(String::Concat("000",strNum),3);
if(String::Compare(Left(strNum,1),"0")!=0){
Result = String::Concat(ConvertDigit(Left(strNum,1))," Hundred ");
}
if(String::Compare(Mid(strNum,1,1),"0")!=0){
Result = String::Concat(Result,ConvertTens(Mid(strNum,1,2)));
}
else{
Result = String::Concat(Result,ConvertDigit(Mid(strNum,2,1)));
}
return Result;
}

String* ConvertCent(String* strNum)
{
String* Result;
if(System::Convert::ToDecimal(strNum)==0) return S"";
strNum = Right(String::Concat("000",strNum),3);
if(String::Compare(Left(strNum,1),"0")!=0){
Result = String::Concat(ConvertChiffre(Left(strNum,1))," Cent ");
}
if(String::Compare(Mid(strNum,1,1),"0")!=0){
Result = String::Concat(Result,ConvertDix(Mid(strNum,1,2)));
}
else{
Result = String::Concat(Result,ConvertChiffre(Mid(strNum,2,1)));
}
return Result;
}

String* ConvertTens(String* strTens)
{
String* sTens;
if(System::Convert::ToInt16(Left(strTens,1))==1){
int nTens = System::Convert::ToInt16(strTens);
switch(nTens){
case 10: {sTens = String::Copy(S"Ten"); break;}
case 11: {sTens = String::Copy(S"Eleven"); break;}
case 12: {sTens = String::Copy(S"Twelve"); break;}
case 13: {sTens = String::Copy(S"Thirteen"); break;}
case 14: {sTens = String::Copy(S"Fourteen"); break;}
case 15: {sTens = String::Copy(S"Fifteen"); break;}
case 16: {sTens = String::Copy(S"Sixteen"); break;}
case 17: {sTens = String::Copy(S"Seventeen"); break;}
case 18: {sTens = String::Copy(S"Eighteen"); break;}
case 19: {sTens = String::Copy(S"Nineteen"); break;}
}
}
else{
switch(System::Convert::ToInt16(Left(strTens,1))){
case 2: {sTens = String::Copy(S"Twenty "); break;}
case 3: {sTens = String::Copy(S"Thirty "); break;}
case 4: {sTens = String::Copy(S"Fourty "); break;}
case 5: {sTens = String::Copy(S"Fifty "); break;}
case 6: {sTens = String::Copy(S"Sixty "); break;}
case 7: {sTens = String::Copy(S"Seventy "); break;}
case 8: {sTens = String::Copy(S"Eighty "); break;}
case 9: {sTens = String::Copy(S"Ninety "); break;}
}
sTens = String::Concat(sTens,ConvertDigit(Right(strTens,1)));
}
return sTens;
}

String* ConvertDix(String* strTens)
{
String* sTens;
if(System::Convert::ToInt16(Left(strTens,1))==1){
int nTens = System::Convert::ToInt16(strTens);
switch(nTens){
case 10: {sTens = String::Copy(S"Dix"); break;}
case 11: {sTens = String::Copy(S"Onze"); break;}
case 12: {sTens = String::Copy(S"Douze"); break;}
case 13: {sTens = String::Copy(S"Treize"); break;}
case 14: {sTens = String::Copy(S"Quatorze"); break;}
case 15: {sTens = String::Copy(S"Quinze"); break;}
case 16: {sTens = String::Copy(S"Seize"); break;}
case 17: {sTens = String::Copy(S"Dix-Sept"); break;}
case 18: {sTens = String::Copy(S"Dix-huit"); break;}
case 19: {sTens = String::Copy(S"Dix-neuf"); break;}
}
}
else{
switch(System::Convert::ToInt16(Left(strTens,1))){
case 2: {sTens = String::Copy(S"Vingt "); break;}
case 3: {sTens = String::Copy(S"Trente "); break;}
case 4: {sTens = String::Copy(S"Quarante "); break;}
case 5: {sTens = String::Copy(S"Cinquante "); break;}
case 6: {sTens = String::Copy(S"Soixante "); break;}
case 7: {sTens = String::Copy(S"Soixante-dix "); break;}
case 8: {sTens = String::Copy(S"Quatre-vingts "); break;}
case 9: {sTens = String::Copy(S"Quatre-vingt-dix "); break;}
}
sTens = String::Concat(sTens,ConvertChiffre(Right(strTens,1)));
}
return sTens;
}

String* ConvertDigit(String* strDigit)
{
String* sDigit;
int nDigit = System::Convert::ToInt16(strDigit);
switch(nDigit){
case 1: {sDigit = String::Copy(S"One"); break;}
case 2: {sDigit = String::Copy(S"Two"); break;}
case 3: {sDigit = String::Copy(S"Three"); break;}
case 4: {sDigit = String::Copy(S"Four"); break;}
case 5: {sDigit = String::Copy(S"Five"); break;}
case 6: {sDigit = String::Copy(S"Six"); break;}
case 7: {sDigit = String::Copy(S"Seven"); break;}
case 8: {sDigit = String::Copy(S"Eight"); break;}
case 9: {sDigit = String::Copy(S"Nine"); break;}
default: {sDigit = String::Copy(S""); break;}
}
return sDigit;
}

String* ConvertChiffre(String* strDigit)
{
String* sDigit;
int nDigit = System::Convert::ToInt16(strDigit);
switch(nDigit){
case 1: {sDigit = String::Copy(S"Un"); break;}
case 2: {sDigit = String::Copy(S"Deux"); break;}
case 3: {sDigit = String::Copy(S"Trois"); break;}
case 4: {sDigit = String::Copy(S"Quatre"); break;}
case 5: {sDigit = String::Copy(S"Cinq"); break;}
case 6: {sDigit = String::Copy(S"Six"); break;}
case 7: {sDigit = String::Copy(S"Sept"); break;}
case 8: {sDigit = String::Copy(S"Huit"); break;}
case 9: {sDigit = String::Copy(S"Neuf"); break;}
default: {sDigit = String::Copy(S""); break;}
}
return sDigit;
}
public:
String* ConvertCurrencyToEnglish(String* MyNumber)
{
String* Temp;
String* Dollars;
String* Cents="";
int Count = 1, DecimalPlace;
MyNumber = MyNumber->Trim();
if(!IsValidNumber(MyNumber)) return S"Given number is wrong!";
if(String::Compare(Left(MyNumber,1),".")==0)
MyNumber = String::Concat("0",MyNumber);
DecimalPlace = SearchStr(MyNumber,".");
if(DecimalPlace > 0 && MyNumber->Length == DecimalPlace+1)
MyNumber = Left(MyNumber,DecimalPlace);
if(DecimalPlace > 0 && MyNumber->Length > DecimalPlace+1){
Temp = Left(String::Concat(Mid(MyNumber,DecimalPlace+1,MyNumber->Length-(DecimalPlace+1)),"00"),2);
Cents = ConvertTens(Temp);
MyNumber = Left(MyNumber,DecimalPlace);
}
String* Place[] ={S"",S"",S" Thousand ",S" Million ",S" Billion ",S" Trillion "};
while(String::Compare(MyNumber,"") != 0){
Temp = ConvertHundreds(Right(MyNumber,3));
if(String::Compare(Temp,"") != 0){
Dollars = String::Concat(String::Concat(Temp,Place[Count]),Dollars);
}
if(MyNumber->Length > 3)
MyNumber = Left(MyNumber,MyNumber->Length-3);
else
MyNumber = "";
Count = Count + 1;
}
if(String::Compare(Dollars,"")==0)
Dollars = "No Dollars";
else if(String::Compare(Dollars,"One")==0)
Dollars = "One Dollar";
else Dollars = String::Concat(Dollars," Dollars");

if(String::Compare(Cents,"")==0)
Cents = " And No Cents";
else if(String::Compare(Cents,"One")==0)
Cents = " And One Cent";
else Cents = String::Concat(" And ",String::Concat(Cents," Cents "));

return String::Concat(Dollars,Cents);
}

String* ConvertCurrencyToFrench(String* MyNumber)
{
String* Temp;
String* Dollars;
String* Cents="";
int Count = 1, DecimalPlace;
MyNumber = MyNumber->Trim();
if(!IsValidNumber(MyNumber)) return S"Given number is wrong!";
if(String::Compare(Left(MyNumber,1),".")==0)
MyNumber = String::Concat("0",MyNumber);
DecimalPlace = SearchStr(MyNumber,".");
if(DecimalPlace > 0 && MyNumber->Length == DecimalPlace+1)
MyNumber = Left(MyNumber,DecimalPlace);
if(DecimalPlace > 0 && MyNumber->Length > DecimalPlace+1){
Temp = Left(String::Concat(Mid(MyNumber,DecimalPlace+1,MyNumber->Length-(DecimalPlace+1)),"00"),2);
Cents = ConvertDix(Temp);
MyNumber = Left(MyNumber,DecimalPlace);
}
String* Place[] ={S"",S"",S" Mille ",S" Million ",S" Milliard ",S" Trillion "};
while(String::Compare(MyNumber,"") != 0){
Temp = ConvertCent(Right(MyNumber,3));
if(String::Compare(Temp,"") != 0){
Dollars = String::Concat(String::Concat(Temp,Place[Count]),Dollars);
}
if(MyNumber->Length > 3)
MyNumber = Left(MyNumber,MyNumber->Length-3);
else
MyNumber = "";
Count = Count + 1;
}
if(String::Compare(Dollars,"")==0)
Dollars = "Non Dollars";
else if(String::Compare(Dollars,"One")==0)
Dollars = "Un Dollar";
else Dollars = String::Concat(Dollars," Dollars");

if(String::Compare(Cents,"")==0)
Cents = " et Non Cents";
else if(String::Compare(Cents,"One")==0)
Cents = " et Un Cent";
else Cents = String::Concat(" et ",String::Concat(Cents," Cents "));

return String::Concat(Dollars,Cents);
}
};
}
Wahoodlum wrote re: Convert numbers to text
on 12-12-2005 5:05 PM
does anyone have a version of this in C# (CSharp)
NeVaL wrote re: Convert numbers to text
on 12-28-2006 6:11 AM

http://www.lukiolaisliiton-kotisivu.beibi.info ^^^ http://www.lukiolaisliitton-kotisivu.beibi.info ^^^ http://www.feit-penis-video.biseksuell.info ^^^ http://www.video-gutter-ung.biseksuell.info ^^^ http://www.feit-penis-video.erotiska.info ^^^ http://www.video-gutter-ung.erotiska.info ^^^ http://www.norsk-snusk.fitta69.info ^^^ http://www.handtralla-nudist-porr.fitta69.info ^^^ http://www.homosexuell-nudist-galleri.fotsex.info ^^^ http://www.porr-lnkar-jpg.fotsex.info ^^^ http://www.alaston-porno-otos.isomuna.info ^^^ http://www.tarve-otos.isomuna.info ^^^ http://www.naken-kuuba-pillua.laukeaminen.info ^^^ http://www.kela-homo-porno.laukeaminen.info ^^^ http://www.portretter-brutal-cocks.rype.info ^^^ http://www.gratis-kjendiser-nakne.rype.info ^^^ http://www.xxx-lesbian.sadsprut.info ^^^ http://www.video-barne-porno.sadsprut.info ^^^ http://www.penis-usshemale-otos.tytsy.info ^^^ http://www.leike-knulla-analen.tytsy.info ^^^ http://www.ass-man-mpg.18analsex.com ^^^ http://www.gals-ass-toys.18analsex.com ^^^ http://www.anziane-lesbica-filmati.pazzesesso.com ^^^ http://www.sesso-negra-allupate.pazzesesso.com ^^^ http://www.clip-vecchio-lions.figanere.com ^^^ http://www.mpg-negras-gordas.figanere.com ^^^ http://www.avi-tetta-bagnata.inculatexxx.com ^^^ http://www.sesso-ragazza-minigonna.inculatexxx.com ^^^ http://www.download-foto-immagini.prostitutaculo.com ^^^ http://www.gratis-amanda-stronza.prostitutaculo.com ^^^ http://www.uomo-bisessuale-avi.lesbicastrip.com ^^^ http://www.gratis-scolarette-giapponese.lesbicastrip.com ^^^ http://www.rubias19-pelirroja-fotos.007sexogratis.com ^^^ http://www.movie-erotica-2006.007sexogratis.com ^^^ http://www.colegialas-prostitutas-jpg.3sexogratis.com ^^^ http://www.contacto-amateur-catalog.3sexogratis.com ^^^ http://www.mature-video-clip.analsexogratis.com ^^^ http://www.gals-mamas-gordas.analsexogratis.com ^^^ http://www.calentorros-clips.cam-sexo-gratis.com ^^^ http://www.paginas-de-sexo.cam-sexo-gratis.com ^^^

How to retrieve a value from FormStringControl before users save r | keyongtech wrote How to retrieve a value from FormStringControl before users save r | keyongtech
on 01-21-2009 8:19 PM

Pingback from  How to retrieve a value from FormStringControl before users save r | keyongtech

Add a Comment

(required)  
(optional)
(required)  
Remember Me?