绘制双精度数并显示小数位?
public class ExperienceTable
{
public static int MaxExperiencePoints;
public static double PercentToNextLevel;
public static void checkLevel(Hero player)
{
if (player.ExperiencePoints >= 0)
{
player.Level = 1;
MaxExperiencePoints = 15;
PercentToNextLevel = player.ExperiencePoints / MaxExperiencePoints;
}
}
然后使用以下方法将其绘制到屏幕上:
GameRef.SpriteBatch.DrawString(GUIFont, "" + ExperienceTable.PercentToNextLevel, new Vector2((int)player.Camera.Position.X + 1200, (int)player.Camera.Position.Y + 676), Color.White);
为什么小数位不显示?似乎数字正在四舍五入。
public class ExperienceTable
{
public static int MaxExperiencePoints;
public static double PercentToNextLevel;
public static void checkLevel(Hero player)
{
if (player.ExperiencePoints >= 0)
{
player.Level = 1;
MaxExperiencePoints = 15;
PercentToNextLevel = player.ExperiencePoints / MaxExperiencePoints;
}
}
Then drawing it to the screen using:
GameRef.SpriteBatch.DrawString(GUIFont, "" + ExperienceTable.PercentToNextLevel, new Vector2((int)player.Camera.Position.X + 1200, (int)player.Camera.Position.Y + 676), Color.White);
How come decimal places don't show up? Seems like the numbers are being rounded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,如果双精度数首先已经是四舍五入的数字,则不会显示小数位。我猜
player.ExperiencePoints
是一个int
。int
除以另一个int
将始终得到一个int
,从而得到一个四舍五入的值。假设
player.ExperiencePoints
实际上是一个int
,并且您希望在除它时得到分数,您应该将除法线更改为以下内容:如果您想要即使它是
.00
,也显示小数位,然后将ExperienceTable.PercentToNextLevel
更改为如下所示。.ToString("0.00")
会将双精度值转换为具有 2 位小数的字符串,并在必要时将其四舍五入到两位小数。By default, the decimal place will not be shown if the double is already a rounded number in the first place. I'm guessing
player.ExperiencePoints
is anint
. Andint
divided by anotherint
will always result to anint
, resulting in a rounded value.Assuming that
player.ExperiencePoints
is really anint
, and you want to have the fraction when dividing it, you should change the division line to the following:And if you want to have the decimal places displayed eventhough it's
.00
, then change theExperienceTable.PercentToNextLevel
to something like the following..ToString("0.00")
will convert the double value to a string with 2 decimal places, and will round it to two decimal places if it have to.我认为
是一个整数除法(如果
player.ExperiencePoints
是一个整数)。尝试一下:
I think
is an integer division (that's if
player.ExperiencePoints
is an integer).Try that:
如果
player.ExperiencePoints
也是一个int
,那么您将一个整数除以一个整数,结果是一个四舍五入的整数。相反,使用If
player.ExperiencePoints
is also anint
, then you are dividing an integer by an integer, and the result is a rounded integer. Instead, use