dianaemanuel Posted May 13, 2013 at 06:31 PM Report #507195 Posted May 13, 2013 at 06:31 PM Boa tarde, Na minha aplicação tenho a seguinte conta: -(-101073.76) - 99503.02 - 681.88 e o valor que está a dar em vb é de 888.85999999999069, alguém me consegue explicar o porquê de dar este resultado e não 888.86??
thoga31 Posted May 13, 2013 at 07:45 PM Report #507199 Posted May 13, 2013 at 07:45 PM Por causa da precisão do tipo de dados numérico que estás a utilizar. É normal haver essa flutuação. http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c Knowledge is free!
dianaemanuel Posted May 14, 2013 at 08:34 AM Author Report #507231 Posted May 14, 2013 at 08:34 AM O resultado final estou a guardar numa variável do tipo double mas também já tentei colocar numa variável do dipo decimal e dá-me exactamente o mesmo valor.
nelsonr Posted May 14, 2013 at 08:46 AM Report #507238 Posted May 14, 2013 at 08:46 AM Experimenta trabalhar com o tipo decimal em vez do double. Algo do tipo: -(DirectCast(-101073.76, decimal)) - DirectCast(99503.02, decimal) - DirectCast(681.88, decimal)
dianaemanuel Posted May 14, 2013 at 09:02 AM Author Report #507241 Posted May 14, 2013 at 09:02 AM Ok, já funcionou. Agora o problema é que em todo o código estou a utilizar sempre variáveis do tipo double, nunca uso decimal, qual é a consequência de alterar tudo para decimal? Alguns dados estão a ser gravados numa base de dados access onde o tipo de campo é double.
thoga31 Posted May 14, 2013 at 06:26 PM Report #507354 Posted May 14, 2013 at 06:26 PM (edited) Se a precisão com muitas casas decimais não é problema, o Double serve bem. Fazes uns Rounds com as casas decimais que precisas (não aconselho mais que 10), e fica tudo a bater certo. nunca uso decimal, qual é a consequência de alterar tudo para decimal? Do site que te indiquei 😉 As for what to use when: For values which are "naturally exact decimals" it's good to use decimal. This is usually suitable for any concepts invented by humans: financial values are the most obvious example, but there are others too. Consider the score given to divers or ice skaters, for example. For values which are more artefacts of nature which can't really be measured exactly anyway, float/double are more appropriate. For example, scientific data would usually be represented in this form. Here, the original values won't be "decimally accurate" to start with, so it's not important for the expected results to maintain the "decimal accuracy". Floating binary point types are much faster to work with than decimals. Precision is the main difference. Float - 7 digits (32 bit) Double-15-16 digits (64 bit) Decimal -28-29 significant digits (128 bit) Decimals have much higher precision and are usually used within financial applications that require a high degree of accuracy. Resumidamente: se usares tudo em Decimal, tens muito maior precisão nos cálculos. Mas se a precisão não é mesmo o mais importante, o meu conselho está dado no início do post. Edited May 14, 2013 at 06:28 PM by thoga31 Knowledge is free!
dianaemanuel Posted May 15, 2013 at 02:17 PM Author Report #507482 Posted May 15, 2013 at 02:17 PM Muito obrigado, já fiquei esclarecida e sendo assim é melhor começar a usar as variáveis do tipo decimal porque para o caso preciso que os resultados dos cálculos sejam precisos.
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