C#错误或错误的东西 [英] C# Bug or Something Wrong

查看:87
本文介绍了C#错误或错误的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有C#不能正确计算我画我的进度条问题

I have an issue with C# not calculating correctly for me to draw my progress bar.

int width = 130;
int maxValue = 20;
int value = 20;

int percent = (width / maxValue) * value

现在应该返回130,意味着我的进度条满,但它返回120,所以我不知道发生了什么。

Now it should return 130 so it mean my progress bar is full but it returns 120 so I don't know what is happening.

下面是和进度条的 http://imgur.com/sUbshxk

我还测试了公式用VB.NET它完美地工作。

I also tested the formula with VB.NET and it worked perfectly.

我使用VS2013在Windows 7 x86的。

I am using VS2013 in Windows 7 x86.

推荐答案

二十分之一百三十零 整数divison

从的 / 运算符(C#参考)

From / Operator (C# Reference)

在计算两个整数,结果始终是一个整数。对于
例如,7/3的结果是2

When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2

这就是为什么它总是舍弃小数部分,并返回 6 。这就是为什么你的结果将是 6 * 20 等于 120

That's why it always discards the fractional part and it returns 6. That's why your result will be 6 * 20 which is equal to 120.

作为一种解决方案,您可以将您的整数除法的改变的浮点除法的。

As a solution, you can change your integer division to floating-point division.

例如;

var percent = (130.0 / 20) * 20;
var percent = (130 / 20.0) * 20;



这意味着,你需要定义变量中的一个作为,或投其中一人双击在你的计算。

That means, you need to define one of your variable as a double, or cast one of them to double in your calculation.

这篇关于C#错误或错误的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆