Hi there,
I would like to convert numbers to words, if anything is ready in PB please let me know, or advice.
Thank you
Hi there,
I would like to convert numbers to words, if anything is ready in PB please let me know, or advice.
Thank you
I found this function a long time a go. Check it out.
global type num2dollar from function_object
end type
forward prototypes
global function string num2dollar (decimal number_to_convert)
end prototypes
global function string num2dollar (decimal number_to_convert);STRING ls_ones, ls_teen, ls_tens, ls_number_string, ls_converted, ls_cents
ls_ones = " One Two ThreeFour Five Six SevenEightNine "
ls_teen = "Ten Eleven Twelve Thirteen Fourteen Fifteen Sixteen SeventeenEightteen Nineteen "
ls_tens = "Twenty Thirty Forty Fifty Sixty SeventyEighty Ninety"
// Store whole dollar amount to a string
If number_to_convert < 0 Then
number_to_convert = number_to_convert * -1
End If
ls_number_string = String ( number_to_convert, "000000000.00" )
//---------------------
// Check for Hundred(s) of MILLION(s)
If DEC ( Mid ( ls_number_string, 1, 1 ) ) > 0 Then
ls_converted = Trim ( Mid ( ls_ones, DEC ( Mid ( ls_number_string, 1, 1) ) *5+1, 5 ) ) + " Hundred "
If Mid ( ls_number_string, 2, 2 ) = "00" Then
ls_converted = ls_converted + "Million "
End If
End If
// Check for Ten(s) of million(s)
If DEC ( Mid ( ls_number_string, 2, 1 ) ) > 1 Then
ls_converted = ls_converted + Trim ( Mid ( ls_tens, DEC ( Mid ( ls_number_string, 2, 1 ) ) *7 - 13, 7 ) )
IF DEC ( Mid ( ls_number_string, 3, 1 ) ) > 0 THEN
ls_converted = ls_converted + "-" + Trim ( Mid ( ls_ones, DEC ( Mid ( ls_number_string, 3, 1 ) ) *5+1, 5 ) )+ " Million "
ELSE
ls_converted = ls_converted + " Million "
End If
ElseIf DEC ( Mid ( ls_number_string, 2, 1 ) ) = 1 Then
ls_converted = ls_converted + Trim ( Mid ( ls_teen, DEC ( Mid ( ls_number_string, 3, 1 ) ) *9+1, 9) ) + " "
Else
IF DEC ( Mid ( ls_number_string, 3, 1 ) ) > 0 Then
ls_converted = ls_converted + Trim ( Mid ( ls_ones, DEC ( Mid ( ls_number_string, 3, 1 ) ) *5+1, 5 ) ) + " Million "
END IF
End If
// -------------------
// Check for Hundred(s) of Thousand(s)
If DEC ( Mid ( ls_number_string, 4, 1 ) ) > 0 Then
ls_converted = ls_converted + Trim ( Mid ( ls_ones, DEC ( Mid ( ls_number_string, 4, 1) ) *5+1, 5 ) ) + " Hundred "
If Mid ( ls_number_string, 5, 2 ) = "00" Then
ls_converted = ls_converted + "Thousand "
End If
End If
// Check for Ten(s) of Thousand(s)
If DEC ( Mid ( ls_number_string, 5, 1 ) ) > 1 Then
ls_converted = ls_converted + Trim ( Mid ( ls_tens, DEC ( Mid ( ls_number_string, 5, 1 ) ) *7 - 13, 7 ) )
If DEC ( Mid ( ls_number_string, 6, 1 ) ) > 0 Then
ls_converted = ls_converted + "-" + Trim ( Mid ( ls_ones, DEC ( Mid ( ls_number_string, 6, 1 ) ) *5+1, 5 ) )
End If
ls_converted = ls_converted + " Thousand "
ElseIf DEC ( Mid ( ls_number_string, 5, 1 ) ) = 1 Then
ls_converted = ls_converted + Trim ( Mid ( ls_teen, DEC ( Mid ( ls_number_string, 6, 1 ) ) *9+1, 9) ) + " Thousand "
//CAMBIE EL SIGNO DE = A > Y EL VALOR LO CAMBIE DE 1 A 0 EN LINEA 56
ElseIf DEC ( Mid ( ls_number_string, 6, 1 ) ) > 0 Then
ls_converted = ls_converted + Trim ( Mid ( ls_ones, DEC ( Mid ( ls_number_string, 6, 1 ) ) *5+1, 5 ) ) + " Thousand "
End If
// Check for Hundred(s)
If Mid ( ls_number_string, 7, 1 ) > "0" Then
ls_converted = ls_converted + Trim ( Mid ( ls_ones, DEC ( Mid ( ls_number_string, 7, 1 ) ) *5+1, 5 ) ) + " Hundred "
End If
// Check for Tens and Ones HUNDRED
If DEC ( Mid ( ls_number_string, 8, 1 ) ) > 1 Then
ls_converted = ls_converted + Trim ( Mid ( ls_tens, DEC ( Mid ( ls_number_string, 8, 1 ) ) *7-13, 7 ) )
If DEC ( Mid ( ls_number_string, 9, 1 ) ) > 0 Then
ls_converted = ls_converted + "-" + Trim ( Mid ( ls_ones, DEC ( Mid ( ls_number_string, 9, 1 ) ) *5+1, 5 ) )
End If
ElseIf DEC ( Mid ( ls_number_string, 8, 1 ) ) = 1 Then
ls_converted = ls_converted + Trim ( Mid ( ls_teen, DEC ( Mid ( ls_number_string, 9, 1 ) ) *9+1, 9 ) )
Else
ls_converted = ls_converted + Trim ( Mid ( ls_ones, DEC ( Mid ( ls_number_string, 9, 1 ) ) *5+1, 5 ) ) //ESTA
End If
// Convert DEC places to a string containing cents
ls_cents = Mid ( ls_number_string, 11, 2 )
If ls_converted = "" Then
ls_converted = "Zero"
End If
ls_converted = ls_converted + " and " + ls_cents + "/100 Dollars"
RETURN ls_converted
end function
I had written a function to do this many years ago, but am unable to find it. However, Chris Pollach's STD framework (available here) contains a number-to-words global function.
I have exported, zip'd and attached this function object from the STD libraries. Please give Chris the attribution for this code.
Best regards, John
There is no such function in PowerBuilder but you may find algorithms in web.
e.g. https://www.geeksforgeeks.org/convert-number-to-words/