1. Mohammad Almasri
  2. PowerBuilder
  3. Tuesday, 26 July 2022 08:01 AM UTC

Hi there,

I would like to convert numbers to words, if anything is ready in PB please let me know, or advice.

Thank you

Clarence Chamorro Accepted Answer Pending Moderation
  1. Thursday, 28 July 2022 15:14 PM UTC
  2. PowerBuilder
  3. # 1

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

Comment
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Tuesday, 26 July 2022 13:57 PM UTC
  2. PowerBuilder
  3. # 2

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

 

Attachments (1)
Comment
  1. Chris Pollach @Appeon
  2. Tuesday, 26 July 2022 14:19 PM UTC
Yes it does! Thanks John for pointing Mohammed to the framework.

Mohammed, the framework is free & open source. Please feel free to utilize whatever code helps you.
  1. Helpful 1
There are no comments made yet.
René Ullrich Accepted Answer Pending Moderation
  1. Tuesday, 26 July 2022 08:11 AM UTC
  2. PowerBuilder
  3. # 3

There is no such function in PowerBuilder but you may find algorithms in web.

e.g. https://www.geeksforgeeks.org/convert-number-to-words/

 

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.