Slammer Posted June 23, 2008 Report Share Posted June 23, 2008 Boa tarde, venho aqui colocar uma dúvida para ver se alguém me pode ajudar. A duvida e a seguinte: Na minha aplicação uso o Math.Round para arredondar alguns valores, por exemplo: Math.Round(0.605) retorna-me o valor "1". Não conhecem nenhuma maneira de eu conseguir o valor "0.61"? Muito obrigado e espero por resposta Abraço Link to comment Share on other sites More sharing options...
vitortomaz Posted June 23, 2008 Report Share Posted June 23, 2008 A função Round tem vários overloads, escolhe a que te permite fazer isso, parece-se que a Round(Decimal, Int32) já resolve http://msdn.microsoft.com/en-us/library/system.math.round.aspx http://vitortomaz.blogspot.com/ Link to comment Share on other sites More sharing options...
Slammer Posted June 24, 2008 Author Report Share Posted June 24, 2008 A função Round tem vários overloads, escolhe a que te permite fazer isso, parece-se que a Round(Decimal, Int32) já resolve http://msdn.microsoft.com/en-us/library/system.math.round.aspx Alterei o codigo para: Math.Round(variavel, 2); e Corta-me apenas a 3ª casa decimal ficando "0.60" Link to comment Share on other sites More sharing options...
vitortomaz Posted June 24, 2008 Report Share Posted June 24, 2008 Math.Round(0.606,2) já retorna 0.61 ? http://vitortomaz.blogspot.com/ Link to comment Share on other sites More sharing options...
vitortomaz Posted June 24, 2008 Report Share Posted June 24, 2008 Math.Round(3.44, 1) 'Returns 3.4. Math.Round(3.45, 1) 'Returns 3.4. Math.Round(3.46, 1) 'Returns 3.5. Math.Round(4.34, 1) ' Returns 4.3 Math.Round(4.35, 1) ' Returns 4.4 Math.Round(4.36, 1) ' Returns 4.4 Tem a ver com o valor anterior ser par ou ímpar se for par arredonda para baixo, se for ímpar arredonda para cima O comportamento desse método segue o padrão IEEE 754 http://vitortomaz.blogspot.com/ Link to comment Share on other sites More sharing options...
Slammer Posted June 25, 2008 Author Report Share Posted June 25, 2008 Então não deve haver grande volta a dar, vou continuar a investigar para ver no que dá. Caso encontre alguma coisa posto aqui, até lá obrigado pelo ajuda. Abraço Link to comment Share on other sites More sharing options...
Betovsky Posted June 25, 2008 Report Share Posted June 25, 2008 Slammer qual é o problema? Queres que arredonde sempre para cima? Já experimentas-te: Math.Round(0.605, 2, MidpointRounding.AwayFromZero); "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk Link to comment Share on other sites More sharing options...
Slammer Posted June 25, 2008 Author Report Share Posted June 25, 2008 Slammer qual é o problema? Queres que arredonde sempre para cima? Já experimentas-te: Math.Round(0.605, 2, MidpointRounding.AwayFromZero); Já tinha experimentado mas dá-me erro no MidpointRounding, diz que não existe no contexto. 😉 Link to comment Share on other sites More sharing options...
Betovsky Posted June 25, 2008 Report Share Posted June 25, 2008 Já tinha experimentado mas dá-me erro no MidpointRounding, diz que não existe no contexto. 😉 ?Estás a usar o .NET 1.1 ? "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk Link to comment Share on other sites More sharing options...
Slammer Posted June 25, 2008 Author Report Share Posted June 25, 2008 ? Estás a usar o .NET 1.1 ? Não, estou a utilizar .NetCompactFrameWork 2.0 SP2 Link to comment Share on other sites More sharing options...
Betovsky Posted June 25, 2008 Report Share Posted June 25, 2008 Estás com azar. Isso não há para o .NET CF "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk Link to comment Share on other sites More sharing options...
Slammer Posted June 25, 2008 Author Report Share Posted June 25, 2008 Pois, era o que estava mesmo agora a ler lol. Bem obrigado na mesma, vou ter de arranjar outra solução. Abraço Link to comment Share on other sites More sharing options...
Slammer Posted June 25, 2008 Author Report Share Posted June 25, 2008 Bem, visto que nesta framework nao existe o MidpointRounding, a solução que arranjei foi somar 0.0001 à variavel que queria arredondar. Por exemplo: decimal variavel = 0.6050; totaldoc = variavel + 0.0001; //ficava 0.6051 totaldoctextbox.text = Convert.ToString(Math.Round(variavel,2)); e assim já funciona. Muito obrigado pela ajuda de todos. Abraço PS: Podem encerrar o tópico Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now