Slammer Posted June 23, 2008 at 04:32 PM Report Share #193080 Posted June 23, 2008 at 04:32 PM 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 at 05:28 PM Report Share #193090 Posted June 23, 2008 at 05:28 PM 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 at 05:35 PM Author Report Share #193259 Posted June 24, 2008 at 05:35 PM 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 at 07:39 PM Report Share #193283 Posted June 24, 2008 at 07:39 PM 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 at 09:06 PM Report Share #193307 Posted June 24, 2008 at 09:06 PM 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 at 08:52 AM Author Report Share #193402 Posted June 25, 2008 at 08:52 AM 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 at 09:16 AM Report Share #193405 Posted June 25, 2008 at 09:16 AM 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 at 09:36 AM Author Report Share #193412 Posted June 25, 2008 at 09:36 AM 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 at 09:46 AM Report Share #193413 Posted June 25, 2008 at 09:46 AM 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 at 09:50 AM Author Report Share #193414 Posted June 25, 2008 at 09:50 AM ? 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 at 09:57 AM Report Share #193416 Posted June 25, 2008 at 09:57 AM 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 at 10:01 AM Author Report Share #193418 Posted June 25, 2008 at 10:01 AM 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 at 12:25 PM Author Report Share #193441 Posted June 25, 2008 at 12:25 PM 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