kapiaba Posted June 18, 2014 Report Share Posted June 18, 2014 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, Quote Link to comment Share on other sites More sharing options...
Theotokos Posted June 18, 2014 Report Share Posted June 18, 2014 tenta assim: nImp := Round( (qt * preco) , 6 ) Quote Link to comment Share on other sites More sharing options...
kapiaba Posted June 18, 2014 Author Report Share Posted June 18, 2014 tenta assim: nImp := Round( (qt * preco) , 6 ) Não deu, retorna: 30.135000 -> deveria retornar: 30.14 Obg. abs. Quote Link to comment Share on other sites More sharing options...
Theotokos Posted June 18, 2014 Report Share Posted June 18, 2014 Não deu, retorna: 30.135000 -> deveria retornar: 30.14 Obg. abs.. round( (qt * preco), 2) --> round( valor, decimal ) Quote Link to comment Share on other sites More sharing options...
kapiaba Posted June 18, 2014 Author Report Share Posted June 18, 2014 round( (qt * preco), 2) --> round( valor, decimal ) KKK, o Vey() eu coloquei o exemplo, é porque eu já testei vária probabilidades, assim retorna: 30.13 e o certo é 30.14 abs, Quote Link to comment Share on other sites More sharing options...
Theotokos Posted June 18, 2014 Report Share Posted June 18, 2014 KKK, o Vey() eu coloquei o exemplo, é porque eu já testei vária probabilidades, assim retorna: 30.13 e o certo é 30.14 abs, é que assim no meu retornou 30.14.... peguei o seu exemplo e modifiquei. pode ser o SET DECIMALS TO Quote Link to comment Share on other sites More sharing options...
Erciley Junior Posted June 18, 2014 Report Share Posted June 18, 2014 Compilando com xHb 1.1, o primeiro resultado retorna o que deveria, ou seja 30.135 O segundo retorna 30.14, pois nesse caso você fez o Round. Não fazendo ROUND na atribuição, sua declaração vai pro espaço, pelo menos nos testes aqui, ou seja, não percebi nenhum erro. Quote Link to comment Share on other sites More sharing options...
Theotokos Posted June 18, 2014 Report Share Posted June 18, 2014 Olha ai! Quote Link to comment Share on other sites More sharing options...
kapiaba Posted June 18, 2014 Author Report Share Posted June 18, 2014 O Sérgio Oliveira, passou uma solução assim: Isto é ou não uma falha do xHabrour?? abs. qt := 2.0500 preco:= 14.7000 Round(Val(StrZero(preco*qt,12,2)),4) Quote Link to comment Share on other sites More sharing options...
kapiaba Posted June 18, 2014 Author Report Share Posted June 18, 2014 Completando, eu estou usando: xHarbour 1.2.3 Intl. (SimpLex) (Build 20130422) Copyright 1999-2013, http://www.xharbour.org http://www.harbour-project.org/ abs, Quote Link to comment Share on other sites More sharing options...
emotta Posted June 18, 2014 Report Share Posted June 18, 2014 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, Quote Link to comment Share on other sites More sharing options...
kapiaba Posted June 18, 2014 Author Report Share Posted June 18, 2014 ? Str(nImp,12,6) // veja como apresenta aqui. Emotta, apresentou: 30.135000, o ROUND() não funcionou. abs, Quote Link to comment Share on other sites More sharing options...
emotta Posted June 18, 2014 Report Share Posted June 18, 2014 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, Quote Link to comment Share on other sites More sharing options...
kapiaba Posted June 18, 2014 Author Report Share Posted June 18, 2014 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. Resultado: 30.130000 kkkkkkkkkkkkkkk, phodeo!!! abs Quote Link to comment Share on other sites More sharing options...
SAOliveira Posted June 19, 2014 Report Share Posted June 19, 2014 xHarbour Reference Documentation Function Reference xHarbour Developers Network Round() Rounds a numeric value to a specified number of digits SyntaxRound( <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. ReturnThe function returns the rounded numeric value. DescriptionRound() 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. InfoSee also: Abs(), Int(), SET DECIMALS, SET FIXED, Str(), Val()Category: Numeric functionsSource: rtl\round.cLIB: xhb.libDLL: 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 Quote Link to comment Share on other sites More sharing options...
joao Posted June 19, 2014 Report Share Posted June 19, 2014 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 Quote Link to comment Share on other sites More sharing options...
Daniel Lopes Filho Posted June 19, 2014 Report Share Posted June 19, 2014 vou procurar uma função que o rossine me passou para truncar o valor e posto aqui e voce testa pode ser ? Quote Link to comment Share on other sites More sharing options...
joao Posted June 20, 2014 Report Share Posted June 20, 2014 Bom dia , claro que sim , agradeço todas as opiniões e soluções. Um abraço João Alpande Quote Link to comment Share on other sites More sharing options...
kapiaba Posted June 20, 2014 Author Report Share Posted June 20, 2014 No xHarbour da Versão FWH 2.7, também não funcionou. abs. Quote Link to comment Share on other sites More sharing options...
sambomb Posted June 20, 2014 Report Share Posted June 20, 2014 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.InfoSee 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. Quote Link to comment Share on other sites More sharing options...
joao Posted June 20, 2014 Report Share Posted June 20, 2014 Boa tarde, Dá o mesmo problema com : Set Decimals to 20 Set Exact ON cumprimentos João Quote Link to comment Share on other sites More sharing options...
kapiaba Posted June 20, 2014 Author Report Share Posted June 20, 2014 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 Quote Link to comment Share on other sites More sharing options...
Erciley Junior Posted June 23, 2014 Report Share Posted June 23, 2014 Realmente Kapiaba, mais um bug da linguagem, vai entender... FUNCTION DECIMAL() a:=30.135 MSGINFO(Round( a , 2 ),'') //RETORNA 30.14 RETURN FUNCTION DECIMAL() qt := 2.0500 preco := 14.7000 a:=qt * preco //2.05 * 14.700000 = 30.135 MSGINFO(Round( a , 2 ),'') //RETORNA 30.13 RETURN Quote Link to comment Share on other sites More sharing options...
kapiaba Posted June 23, 2014 Author Report Share Posted June 23, 2014 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 ); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.