Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 画双精度并显示小数点?_C#_Double_Decimal - Fatal编程技术网

C# 画双精度并显示小数点?

C# 画双精度并显示小数点?,c#,double,decimal,C#,Double,Decimal,然后使用以下命令将其绘制到屏幕: public class ExperienceTable { public static int MaxExperiencePoints; public static double PercentToNextLevel; public static void checkLevel(Hero player) { if (player.ExperiencePoints >= 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;
        }
    }

为什么小数点不出现?看起来这些数字正在四舍五入。

默认情况下,如果第一位的double已经是四舍五入的数字,则不会显示小数点。我猜是
玩家。ExperiencePoints
是一个
int
。而
int
除以另一个
int
将始终得到
int
,从而得到一个四舍五入的值

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

        GameRef.SpriteBatch.DrawString(GUIFont, "" + ExperienceTable.PercentToNextLevel, new Vector2((int)player.Camera.Position.X + 1200, (int)player.Camera.Position.Y + 676), Color.White);
如果希望在
.00
时显示小数点,则将
ExperienceTable.PercentToNextLevel
更改为如下内容

PercentToNextLevel = (double) player.ExperiencePoints / MaxExperiencePoints;
.ToString(“0.00”)
会将双精度值转换为小数点后两位的字符串,如果有必要,会将其四舍五入到小数点后两位。

我认为

ExperienceTable.PercentToNextLevel.ToString("0.00")
是整数除法(如果
player.ExperiencePoints
是整数)

试试看:

PercentToNextLevel = player.ExperiencePoints / MaxExperiencePoints;

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

PercentToNextLevel = (double)player.ExperiencePoints / MaxExperiencePoints;
PercentToNextLevel = (double)player.ExperiencePoints / MaxExperiencePoints