#include "FiveWin.ch"
static oWnd
#xtranslate round(<nVal>,<nDec>) => val(str(<nVal>,20,<nDec>))
FUNCTION Main()
local oBar
DEFINE WINDOW oWnd TITLE "TestE Round()"
DEFINE BUTTONBAR oBar _3D OF oWnd
DEFINE BUTTON OF oBar ACTION Test_Round()
SET MESSAGE OF oWnd TO "TestE Round()" NOINSET CLOCK DATE KEYBOARD
ACTIVATE WINDOW oWnd
RETURN NIL
FUNCTION Test_Round()
LOCAL nImp, qt, preco, nNewImp, nNewValor
nNewValor := 0
nImp := 0.00
qt := 2.0500
preco := 14.7000
//nImp := Round( (qt * preco) , 2 )
nImp := ( ROUND( qt, 2 ) ) * ( ROUND( preco, 2 ) )
? ROUND( nImp, 2 )
? Str(Round(nImp,2),12,6) // veja como apresenta aqui
nImp := Round(Val(StrZero(preco*qt,12,2)),4)
? ROUND( nImp, 2) // Deveria retornar 30.14 e nao esta... Retorna 30.13
nNewImp := PADR( nImp, 6 )
nImp := VAL( PADR( nNewImp, 6 ) )
nNewValor := ROUND( nImp, 2 )
? nNewValor // assim retorna correto, 30.14
RETURN NIL
FUNCTION ROUND2(VVALOR,VDEC)
VVALOR=VAL(STR(VVALOR,19,6))
RETURN(VAL(STR(VVALOR,19,VDEC)))
SET DECIMALS TO 2
Msginfo( "Valor inicial : "+Str( 8.00, 5,2 ) + CRLF + ;
"Base Imponible: "+Str( Round( 8/1.21 , 2 ) , 5 , 2 ) + CRLF + ;
"IVA : "+Str( Round(Round( 8/1.21 , 2 ) * 0.21,2) , 5 , 2 ) + CRLF + ;
"Total : "+Str( Round( 8/1.21 , 2 )+Round(Round( 8/1.21 , 2 ) * 0.21,2) , 5 , 2 ) ;
)
Hola Eloy:
A ver si con esta sale: La funcion xRound(), la pongo al final de estas lineas.
MsgInfo(xround( xROUND(7/1.21,3),2 ) )
MsgInfo(xRound( xROUND(7/1.21,3),2 ))
MsgInfo(xRound( xROUND(7/1.21,3),2 ) - 7 )
MsgInfo(xround( xROUND(8/1.21,3),2 ) )
MsgInfo(xRound( xROUND(8/1.21,3),2 ))
MsgInfo(xRound( xROUND(8/1.21,3),2 ) - 8 )
Aparentemente salen ok.
Function xRound( x, y )
Local nDeci := 2
y := 2
nDeci := Set( _SET_DECIMALS, y )
If x >= 0
x := Val( Str( Int( x * 10 ** y + 0.5000001 ) ) ) / 10 ** y
Else
x := Val( Str( Int( x * 10 ** y - 0.5000001 ) ) ) / 10 ** y
Endif
Set( _SET_DECIMALS, nDeci )
Return x
Saludos y espero que funcione.
http://forums.fivetechsupport.com/viewtopic.php?f=6&t=27887&hilit=ROUND
Eloy: La funcion que pongo funciona bien. Fíjate que hago 2 redondeos.
xRound( xRound())
el xRound() de adentro a 3 decimales y el de afuera a 2 decimales.
Primero a 3 decimales y luego a dos, me funciona bien aquí.
MsgInfo(xround( xROUND(7/1.21,3),2 ) )
MsgInfo(xRound( xROUND(7/1.21,3),2 ))
MsgInfo(xRound( xROUND(7/1.21,3),2 ) - 7 )
MsgInfo(xround( xROUND(8/1.21,3),2 ) )
MsgInfo(xRound( xROUND(8/1.21,3),2 ))
MsgInfo(xRound( xROUND(8/1.21,3),2 ) - 8
Saludos
Simplifique assim:
calculo = 1.6752
calculo2 = PADR(calculo,4)
O valor de calculo2 também será 1.67
Se for usar a variável para outros cálculos, basta acrescentar VAL que o valor continuará sendo tipo numérico:
calculo2 = VAL(PADR(calculo,4))
// round normal to 2 decimal places
? ft_Round( nDollars )
// round normal to 6 decimal places
? ft_Round( nIntRate, 6 )
// round to nearest thousands
? ft_Round( nPrice, 3, NEAREST_WHOLE_NUMBER )
// round Up to nearest third
? ft_Round( nAmount, 3, NEAREST_FRACTION, ROUND_UP )
// round down to 3 decimals Within .005
? ft_Round( nAvg, 3, , ROUND_DOWN, .005 )
Ver: CEILLING() do xharbour