绘制双精度数并显示小数位?

发布于 2024-12-09 12:23:21 字数 689 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛上残留的泪 2024-12-16 12:23:21

默认情况下,如果双精度数首先已经是四舍五入的数字,则不会显示小数位。我猜 player.ExperiencePoints 是一个 intint 除以另一个 int 将始终得到一个 int,从而得到一个四舍五入的值。

假设 player.ExperiencePoints 实际上是一个 int,并且您希望在除它时得到分数,您应该将除法线更改为以下内容:

PercentToNextLevel = (double) player.ExperiencePoints / MaxExperiencePoints;

如果您想要即使它是 .00,也显示小数位,然后将 ExperienceTable.PercentToNextLevel 更改为如下所示。

ExperienceTable.PercentToNextLevel.ToString("0.00")

.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 an int. And int divided by another int will always result to an int, resulting in a rounded value.

Assuming that player.ExperiencePoints is really an int, and you want to have the fraction when dividing it, you should change the division line to the following:

PercentToNextLevel = (double) player.ExperiencePoints / MaxExperiencePoints;

And if you want to have the decimal places displayed eventhough it's .00, then change the ExperienceTable.PercentToNextLevel to something like the following.

ExperienceTable.PercentToNextLevel.ToString("0.00")

.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.

半暖夏伤 2024-12-16 12:23:21

我认为

PercentToNextLevel = player.ExperiencePoints / MaxExperiencePoints;

是一个整数除法(如果player.ExperiencePoints是一个整数)。

尝试一下:

PercentToNextLevel = (double)player.ExperiencePoints / MaxExperiencePoints;

I think

PercentToNextLevel = player.ExperiencePoints / MaxExperiencePoints;

is an integer division (that's if player.ExperiencePoints is an integer).

Try that:

PercentToNextLevel = (double)player.ExperiencePoints / MaxExperiencePoints;
甜心小果奶 2024-12-16 12:23:21

如果 player.ExperiencePoints 也是一个 int,那么您将一个整数除以一个整数,结果是一个四舍五入的整数。相反,使用

PercentToNextLevel = (double)player.ExperiencePoints / MaxExperiencePoints

If player.ExperiencePoints is also an int, then you are dividing an integer by an integer, and the result is a rounded integer. Instead, use

PercentToNextLevel = (double)player.ExperiencePoints / MaxExperiencePoints
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文