Jump to content
Fivewin Brasil

FALHA NO COMANDO ROUND() DO XHARBOUR


kapiaba

Recommended Posts

Galera, tem algum problema com o comando ROUND() do xHarbour, não retorna como deveria ser, veja este exemplo:



#include "FiveWin.ch"

static oWnd

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, 6 ) ) * ( ROUND( preco, 6 ) )

? nImp // 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

// ? ROUND( nImp, 2 )

RETURN NIL


abs,

Link to comment
Share on other sites

Isso não é pau do Round... Apenas na visualização, como voce não especificou o numero de casas, ele mostrou o falor sem o arredondamento, mas a variavel está com valor correto.

? nImp // Deveria retornar 30.14 e nao esta... Retorna 30.13

*Nesta situação acima a variavel está com o valor 30.135 só na hora de exibir ele está mostrando sem o arredondamento. Faça um pequeno teste:

? Str(nImp,12,6) // veja como apresenta aqui.

O restante do seu código é a mesma coisa...

Galera, tem algum problema com o comando ROUND() do xHarbour, não retorna como deveria ser, veja este exemplo:
#include "FiveWin.ch"
 
static oWnd
 
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, 6 ) ) * ( ROUND( preco, 6 ) )
 
   ? nImp  // 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
 
   // ? ROUND( nImp, 2 )
 
RETURN NIL
abs,
Link to comment
Share on other sites

Mas vc queria o arredondamento para duas casas? Se é pra duas casas tem que ser ROUND(X,2)

? Str(Round(nImp,2),12,6) // veja como apresenta aqui.

? Str(nImp,12,6) // veja como apresenta aqui.

Emotta, apresentou: 30.135000, o ROUND() não funcionou.

abs,

Link to comment
Share on other sites

xHarbour Reference Documentation

Function Reference xHarbour Developers Network

Round()

Rounds a numeric value to a specified number of digits

Syntax
Round( <nNumber>, <nDecimals> ) --> nRounded

Arguments
<nNumber>
This is the numeric value to round.

<nDecimals>
If the parameter is a positive number, it specifies the number of decimal places to retain after the decimal point. If specified as negative value, Round() operates on the digits before the decimal point, thus rounding integer numbers. Return
The function returns the rounded numeric value.

Description
Round() is a numeric function used to round numbers to a given number of decimal places. Digits 5 to 9 are rounded up, while digits 0 to 4 round down.

Info
See also: Abs(), Int(), SET DECIMALS, SET FIXED, Str(), Val()
Category: Numeric functions
Source: rtl\round.c
LIB: xhb.lib
DLL: xhbdll.dll

Example
// The example demonstrates results of Round() and how SET FIXED
// influences the display of rounded numbers.

PROCEDURE Main

SET DECIMALS TO 4
SET FIXED ON

? Round( 1234.5678, 0) // result: 1235.0000
? Round( 1234.5678, 1) // result: 1234.6000
? Round( 1234.5678, 2) // result: 1234.5700
? Round( 1234.5678, 3) // result: 1234.5680

? Round( 1234.5678,-1) // result: 1230.0000
? Round( 1234.5678,-2) // result: 1200.0000
? Round( 1234.5678,-3) // result: 1000.0000

SET FIXED OFF

? Round( 1234.5678, 0) // result: 1235
? Round( 1234.5678, 1) // result: 1234.6
? Round( 1234.5678, 2) // result: 1234.57
? Round( 1234.5678, 3) // result: 1234.568

? Round( 1234.5678,-1) // result: 1230
? Round( 1234.5678,-2) // result: 1200
? Round( 1234.5678,-3) // result: 1000

RETURN

Link to comment
Share on other sites

Bom dia,

Obrigado João por expor este problema que falamos, fiz o mesmo teste com Harbour e dá certo ,no xHarbour dá errado .

Sérgio A.Oliveira ,

se fizer ? Round( 1234.5678, 2) arredonda bem para 1234.57,mas se fizer um cálculo como tem no teste dá errado

Teste no xHarbour :

nImp := 0.00

qt := 2.0500
preco := 14.7000

nImp := ( ROUND( qt, 2 ) ) * ( ROUND( preco, 2 ) )

? ROUND( nImp, 2 )
// Deveria retornar 30.14 e nao esta... Retorna 30.13

Teste no Harbour :

nImp := 0.00

qt := 2.0500
preco := 14.7000

nImp := ( ROUND( qt, 2 ) ) * ( ROUND( preco, 2 ) )

? ROUND( nImp, 2 )
// retornar 30.14 que está certo
Um Abraço
João Alpande
Link to comment
Share on other sites

Round()

Rounds a numeric value to a specified number of digits

Syntax Round( <nNumber>, <nDecimals> ) --> nRounded

Arguments

<nNumber> This is the numeric value to round. <nDecimals> If the parameter is a positive number, it specifies the number of decimal

places to retain after the decimal point. If specified as negative value, Round() operates on the

digits before the decimal point, thus rounding integer numbers.

Return

The function returns the rounded numeric value.

Description

Round() is a

numeric function used to round numbers to a given number of decimal places. Digits 5

to 9 are rounded

up, while digits 0 to 4 round down.

Info

See also: Abs(), Int(), SET DECIMALS, SET FIXED, Str(), Val() Category: Numeric

functions Source: rtl\round.c LIB: xhb.lib DLL: xhbdll.dll

Example // The example demonstrates results of Round() and how SET FIXED

// influences the display of rounded numbers.

PROCEDURE Main

SET DECIMALS TO 4

SET FIXED ON

? Round( 1234.5678, 0) // result: 1235.0000

? Round( 1234.5678, 1) // result: 1234.6000

? Round( 1234.5678, 2) // result: 1234.5700

? Round( 1234.5678, 3) // result: 1234.5680

? Round( 1234.5678,-1) // result: 1230.0000

? Round( 1234.5678,-2) // result: 1200.0000

? Round( 1234.5678,-3) // result: 1000.0000

SET FIXED OFF

? Round( 1234.5678, 0) // result: 1235

? Round( 1234.5678, 1) // result: 1234.6

? Round( 1234.5678, 2) // result: 1234.57

? Round( 1234.5678, 3) // result: 1234.568

? Round( 1234.5678,-1) // result: 1230

? Round( 1234.5678,-2) // result: 1200

? Round( 1234.5678,-3) // result: 1000

RETURN

Retirando o manual do xHarbour

Tente com o Set Fixed ON e Set Fixed Off para testar ;)

Tente com Set Decimals e Set Exact tb

Quando eu tenho que fazer contas assim costumo fazer:

Set Decimals to 20

Set Exact ON

Depois retorno aos padrões originais.

Link to comment
Share on other sites

Assim funciona redondo.



#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

Link to comment
Share on other sites

No forum do Pctoledo, indicaram este fonte, não testei, se funcionar, resolve op problema para quem usa HARBOUR OU XHABROUR.


Agora se for erro do Borland, laskou-se.. kkkkkkkkkkkkkkkkkkkkk




/*
* Round(), Int() functions
*
* Copyright 1999 David G. Holm <dholm@jsd-llc.com>
* Copyright 1999 Matthew Hamilton <mhamilton@bunge.com.au> (Int())
* Copyright 2003 Vicente Aranzana <varanzana@gruposp.com> (hb_numRound())
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING.txt. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA (or visit the web site https://www.gnu.org/).
*
* As a special exception, the Harbour Project gives permission for
* additional uses of the text contained in its release of Harbour.
*
* The exception is that, if you link the Harbour libraries with other
* files to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the Harbour library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the Harbour
* Project under the name Harbour. If you copy code from other
* Harbour Project or Free Software Foundation releases into a copy of
* Harbour, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for Harbour, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice.
*
*/

#include "hbapi.h"
#include "hbapiitm.h"
#include "hbapierr.h"

HB_FUNC( INT )
{
PHB_ITEM pNumber = hb_param( 1, HB_IT_NUMERIC );

if( pNumber )
{
if( HB_IS_NUMINT( pNumber ) )
hb_itemReturn( pNumber );
else
{
int iWidth;

hb_itemGetNLen( pNumber, &iWidth, NULL );
hb_retnlen( hb_numInt( hb_itemGetND( pNumber ) ), iWidth, 0 );
}
}
else
hb_errRT_BASE_SubstR( EG_ARG, 1090, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}

HB_FUNC( ROUND )
{
PHB_ITEM pNumber = hb_param( 1, HB_IT_NUMERIC );

if( pNumber && HB_ISNUM( 2 ) )
{
int iDec = hb_parni( 2 );

#ifdef HB_CLP_STRICT
/* In CA-Cl*pper Round() always returns double item, what in some
* applications may be important due to different formatting rules
* when SET FIXED is ON [druzus]
*/
hb_retndlen( hb_numRound( hb_itemGetND( pNumber ), iDec ), 0, HB_MAX( iDec, 0 ) );
#else
if( iDec == 0 && HB_IS_NUMINT( pNumber ) )
hb_retnint( hb_itemGetNInt( pNumber ) );
else
hb_retnlen( hb_numRound( hb_itemGetND( pNumber ), iDec ), 0, HB_MAX( iDec, 0 ) );
#endif
}
else
hb_errRT_BASE_SubstR( EG_ARG, 1094, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS );
}



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...