Página 1 de 2
Função Valores em Extenso em Inglês
Enviado: 22 Jul 2014 09:38
por joaoalpande
Boa Tarde,
Alguém tem a função para converter valores em extenso em inglês?
Cumprimentos
João Alpande
Função Valores em Extenso em Inglês
Enviado: 22 Jul 2014 10:09
por Kapiaba
Código: Selecionar todos
/*
* File......: NTOW.PRG
* Author....: Gary Baren
* CIS ID....: 75470,1027
*
* This is an original work by Gary Baren and is hereby placed in the
* public domain.
*
* Modification history:
* ---------------------
*
* Rev 1.1 15 Aug 1991 23:05:54 GLENN
* Forest Belt proofread/edited/cleaned up doc
*
* Rev 1.0 09 Jun 1991 00:26:56 GLENN
* Initial revision.
*
*/
/* $DOC$
* $FUNCNAME$
* FT_NTOW()
* $CATEGORY$
* Conversion
* $ONELINER$
* Translate numeric value to words
* $SYNTAX$
* FT_NTOW( <nNumber> ) -> cWords
* $ARGUMENTS$
* <nNumber> An integer to translate
* $RETURNS$
* A text string representing <nNumber>
* $DESCRIPTION$
* Translates numeric input to a text string.
*
* FT_NTOW is intended to be used with integers only. Since I don't
* know what your application will be, I can't assume the type of
* fraction you want returned (ninety nine cents, 99/100, .99, etc).
* If you want the fraction in words, just pass it as an integer.
*
* Do not pass a negative number! Handle negative numbers any way
* you need to in your code. (ie: CR, DB, Negative, Minus, etc.)
*
* Also, numeric 0 is returned as a null string. You will need to
* make a decision how to output it (zero dollars, no dollars, etc).
* $EXAMPLES$
* ? FT_NTOW( 999 ) -> Nine Hundred Ninety Nine
*
* ? FT_NTOW( 1000 ) -> One Thousand
*
* ? FT_NTOW( 23 ) + " Dollars and " + FT_NTOW( 99 ) + " Cents"
* -> Twenty Three Dollars and Ninety Nine Cents
*
* ? FT_NTOW( 23 ) + " Dollars and " + "99/100"
* -> Twenty Three Dollars and 99/100
*
* x := -23.99
* cents := str( (x - int( x )) * 100, 2, 0 ) + "/100"
* x := int( x )
* string := iif( x < 0, "Credit of ", "Debit of " )
* ? string + FT_NTOW( abs(x) ) + " Dollars and " + "99/100"
* -> Credit of Twenty Three Dollars and 99/100
* $END$
*/
static ones := { "", " One", " Two", " Three", " Four", " Five", ;
" Six", " Seven", " Eight", " Nine" ;
}
static teens := { " Ten", " Eleven", " Twelve", ;
" Thirteen", " Fourteen", " Fifteen", ;
" Sixteen", " Seventeen", " Eighteen", ;
" Nineteen" ;
}
static tens := { "", "", " Twenty", " Thirty", " Forty", " Fifty", ;
" Sixty", " Seventy", " Eighty", " Ninety" }
static qualifiers := { "", " Thousand", " Million", " Billion", " Trillion" }
#ifdef FT_TEST
function main( cNum )
return qout( ft_ntow( val( cNum ) ) )
#endif
function ft_ntow(nAmount)
local nTemp, sResult := " ", nQualNo
local nDiv := 10 ^ ( int( sol10(nAmount) / 3 ) * 3 )
nTemp := int(nAmount % nDiv)
nAmount := int(nAmount / nDiv)
nQualNo := int( sol10( nDiv ) / 3 ) + 1
sResult += grp_to_words(nAmount, qualifiers[ nQualNo ] )
if nTemp > (nDiv /= 1000) .and. (nDiv > 1)
sResult += ft_ntow( nTemp, nDiv )
else
sResult += grp_to_words(nTemp, "")
endif
return( ltrim(sResult) )
static function grp_to_words(nGrp, sQual)
local sResult := "", nTemp
nTemp := int(nGrp % 100)
nGrp := int(nGrp / 100)
sResult += ones[ nGrp + 1 ] + iif( nGrp > 0, " Hundred", "")
do case
case nTemp > 19
sResult += tens[ int( nTemp / 10 ) + 1 ]
sResult += ones[ int( nTemp % 10 ) + 1 ]
case nTemp < 20 .and. nTemp > 9
sResult += teens[ int( nTemp % 10 ) + 1 ]
case nTemp < 10 .and. nTemp > 0
sResult += ones[ int( nTemp) + 1 ]
endcase
return(sResult + sQual)
static function sol10( nNumber )
local sTemp
sTemp := ltrim( str( int(nNumber), 0) )
return( len(sTemp) - 1 )
Função Valores em Extenso em Inglês
Enviado: 22 Jul 2014 10:31
por joaoalpande
Boa Tarde,
Obrigado ,mas chegou a testar? eu já tentei testar essa função que vi no forum da fivetech e só deu o número inteiro , não deu os centimos, sera que fiz alguma coisa errada'
Cumprimentos
João Alpande
Função Valores em Extenso em Inglês
Enviado: 22 Jul 2014 11:10
por joaoalpande
consigui meter os centimos em extenso, só tem um problema esta função,não coloca o - exemplo:
275.49
two hundred seventy-five euros and forty-nine cents
com esta função fica:
two hundred seventy five euros and forty nine cents
cumprmentos
João Alpande
Função Valores em Extenso em Inglês
Enviado: 22 Jul 2014 11:25
por bencz
Função Valores em Extenso em Inglês
Enviado: 22 Jul 2014 11:31
por joaoalpande
Obrigado ,em português tenho a funcionar , só queria saber se alguém tinha essa função para inglês para não ter esse trabalho de adptar, mas se não tiver irei ter esse trabalho.
cumprimentos
João Alpande
Função Valores em Extenso em Inglês
Enviado: 22 Jul 2014 14:39
por Kapiaba
Função Valores em Extenso em Inglês
Enviado: 22 Jul 2014 20:10
por rochinha
Amiguinhos,
Na linha:
Código: Selecionar todos
...
static tens := { "", "", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety" }
Coloque assim:
Código: Selecionar todos
...
static tens := { "", "", " Twenty-", " Thirty-", " Forty-", " Fifty-", " Sixty-", " Seventy-", " Eighty-", " Ninety-" }
Nunca usei estas funções, na verdade tem muitas funções do Harbour que ainda não fiz uso.
Função Valores em Extenso em Inglês
Enviado: 23 Jul 2014 12:51
por Kapiaba
Código: Selecionar todos
#include "FiveWin.ch"
/*
* File......: NTOW.PRG
* Author....: Gary Baren
* CIS ID....: 75470,1027
*
* This is an original work by Gary Baren and is hereby placed in the
* public domain.
*
* Modification history:
* ---------------------
*
* Rev 1.1 15 Aug 1991 23:05:54 GLENN
* Forest Belt proofread/edited/cleaned up doc
*
* Rev 1.0 09 Jun 1991 00:26:56 GLENN
* Initial revision.
*
*/
/* $DOC$
* $FUNCNAME$
* FT_NTOW()
* $CATEGORY$
* Conversion
* $ONELINER$
* Translate numeric value to words
* $SYNTAX$
* FT_NTOW( <nNumber> ) -> cWords
* $ARGUMENTS$
* <nNumber> An integer to translate
* $RETURNS$
* A text string representing <nNumber>
* $DESCRIPTION$
* Translates numeric input to a text string.
*
* FT_NTOW is intended to be used with integers only. Since I don't
* know what your application will be, I can't assume the type of
* fraction you want returned (ninety nine cents, 99/100, .99, etc).
* If you want the fraction in words, just pass it as an integer.
*
* Do not pass a negative number! Handle negative numbers any way
* you need to in your code. (ie: CR, DB, Negative, Minus, etc.)
*
* Also, numeric 0 is returned as a null string. You will need to
* make a decision how to output it (zero dollars, no dollars, etc).
* $EXAMPLES$
* ? FT_NTOW( 999 ) -> Nine Hundred Ninety Nine
*
* ? FT_NTOW( 1000 ) -> One Thousand
*
* ? FT_NTOW( 23 ) + " Dollars and " + FT_NTOW( 99 ) + " Cents"
* -> Twenty Three Dollars and Ninety Nine Cents
*
* ? FT_NTOW( 23 ) + " Dollars and " + "99/100"
* -> Twenty Three Dollars and 99/100
*
* x := -23.99
* cents := str( (x - int( x )) * 100, 2, 0 ) + "/100"
* x := int( x )
* string := iif( x < 0, "Credit of ", "Debit of " )
* ? string + FT_NTOW( abs(x) ) + " Dollars and " + "99/100"
* -> Credit of Twenty Three Dollars and 99/100
* $END$
*/
static ones := { "", " One", " Two", " Three", " Four", " Five", ;
" Six", " Seven", " Eight", " Nine" ;
}
static teens := { " Ten", " Eleven", " Twelve", ;
" Thirteen", " Fourteen", " Fifteen", ;
" Sixteen", " Seventeen", " Eighteen", ;
" Nineteen" ;
}
// Original
static tens := { "", "", " Twenty", " Thirty", " Forty", " Fifty", ;
" Sixty", " Seventy", " Eighty", " Ninety" }
// By Rochinha // Nao entendi, retorna extenso incorreto.
/*
static tens := { "", "", " Twenty-", " Thirty-", " Forty-", " Fifty-", ;
" Sixty-", " Seventy-", " Eighty-", " Ninety-" }
*/
static qualifiers := { "", " Thousand", " Million", " Billion", " Trillion" }
Function Main() //main( cNum )
? FT_NTOW( 100 ) + " Dollars "
? FT_NTOW( 999 ) //-> Nine Hundred Ninety Nine
? FT_NTOW( 1000 ) + " Dollars " //-> One Thousand Dollars
? FT_NTOW( 23 ) + " Dollars and " + FT_NTOW( 99 ) + " Cents"
* -> Twenty Three Dollars and Ninety Nine Cents
? FT_NTOW( 23 ) + " Dollars and " + "99/100"
* -> Twenty Three Dollars and 99/100
Return Nil // qout( ft_ntow( val( cNum ) ) )
function ft_ntow(nAmount)
local nTemp, sResult := " ", nQualNo
local nDiv := 10 ^ ( int( sol10(nAmount) / 3 ) * 3 )
nTemp := int(nAmount % nDiv)
nAmount := int(nAmount / nDiv)
nQualNo := int( sol10( nDiv ) / 3 ) + 1
sResult += grp_to_words(nAmount, qualifiers[ nQualNo ] )
if nTemp > (nDiv /= 1000) .and. (nDiv > 1)
sResult += ft_ntow( nTemp, nDiv )
else
sResult += grp_to_words(nTemp, "")
endif
//? ltrim(sResult)
return( ltrim(sResult) )
static function grp_to_words(nGrp, sQual)
local sResult := "", nTemp
nTemp := int(nGrp % 100)
nGrp := int(nGrp / 100)
sResult += ones[ nGrp + 1 ] + iif( nGrp > 0, " Hundred", "")
do case
case nTemp > 19
sResult += tens[ int( nTemp / 10 ) + 1 ]
sResult += ones[ int( nTemp % 10 ) + 1 ]
case nTemp < 20 .and. nTemp > 9
sResult += teens[ int( nTemp % 10 ) + 1 ]
case nTemp < 10 .and. nTemp > 0
sResult += ones[ int( nTemp) + 1 ]
endcase
return(sResult + sQual)
static function sol10( nNumber )
local sTemp
sTemp := ltrim( str( int(nNumber), 0) )
return( len(sTemp) - 1 )
abs,
Função Valores em Extenso em Inglês
Enviado: 23 Jul 2014 22:02
por rochinha
Amiguinhos,
Óieu aqui traveiz.
Como havia indicado logo acima, de colocar o "-"(hifen) após o qualificador de moeda, e como havia dito nunca tinha usado a função e também porque o nobre colega João Santos frisou que não funcionou, fui fazer meus testes e conhecer a função e saber o que se podia fazer.
Bem na linha abaixo eu coloqui os hifens e percebi que causava um espaço extra na concatenação das palavras então:
Código: Selecionar todos
// By Rochinha // Nao entendi, retorna extenso incorreto.
static tens := { "", "", " Twenty-", " Thirty-", " Forty-", " Fifty-", " Sixty-", " Seventy-", " Eighty-", " Ninety-" }
Habilitei a linha acima e na função FT_NTOW() adicionei funções ao retorno:
Nesta função eu saio em busca de hifen+espaço e troco por hifen somente.
Para mim, ficou saboroso.
Código: Selecionar todos
function ft_ntow(nAmount)
local nTemp, sResult := " ", nQualNo
local nDiv := 10 ^ ( int( sol10(nAmount) / 3 ) * 3 )
nTemp := int(nAmount % nDiv)
nAmount := int(nAmount / nDiv)
nQualNo := int( sol10( nDiv ) / 3 ) + 1
sResult += grp_to_words(nAmount, qualifiers[ nQualNo ] )
if nTemp > (nDiv /= 1000) .and. (nDiv > 1)
sResult += ft_ntow( nTemp, nDiv )
else
sResult += grp_to_words(nTemp, "")
endif
//? ltrim(sResult)
return( strtran(ltrim(sResult),"- ","-") )
Função Valores em Extenso em Inglês
Enviado: 24 Jul 2014 02:31
por rochinha
Amiguinhos,
Fiz uns testes com a Função FT_NTOW() achei interessante, mas como eu já tinha uma função preferi melhorá-la e disponibilizar na sessão
Contribuições, Dicas e Tutoriais:
Segue os links da versão em BRasileiro e ENgles:
FUNÇÃO: Valores extensos até quatrilhão.
FUNÇÃO: Valores extensos até quatrilhão(ENgles).
Função Valores em Extenso em Inglês
Enviado: 24 Jul 2014 20:06
por JoséQuintas
Se a rotina indicada funciona pra números, então basta usá-la duas vezes, uma pra inteiros e outra pra decimais.
Algo mais ou menos assim, mas tratando singular/plural e centavos zero.
Código: Selecionar todos
ft_NTow( Int( nValor ) ) + " DOLAR " + ft_NTow( nValor - Int( nValor ) ) + " CENTS"
Função Valores em Extenso em Inglês
Enviado: 25 Jul 2014 09:30
por Itamar M. Lins Jr.
O Viktor está antenado!
2014-07-24 13:41 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
* contrib/hbnf/ntow.prg
% and some more
2014-07-24 13:37 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
* contrib/hbnf/ntow.prg
% optimizations
2014-07-24 13:30 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
* contrib/hbnf/ntow.prg
! two fixes to prev
2014-07-24 13:15 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
* contrib/hbnf/ntow.prg
! FT_NTOW() fixed original bug missing space separator between certain words
! FT_NTOW() fixed to use '-' instead of space separator between certain words
% minor cleanup
; f.e. try 14234325
Saudações,
Itamar M. Lins Jr.
Função Valores em Extenso em Inglês
Enviado: 25 Jul 2014 10:28
por Jairo Maia
Since 2014-07-24 13:30, until 2014-07-24 13:41. Acho que ele acompanha este fórum. Tomara...
:-Y
Função Valores em Extenso em Inglês
Enviado: 25 Jul 2014 10:34
por JoséQuintas
Este fórum está relacionado nos documentos do Harbour 3.4
Acredito que ele esteja acompanhando.