Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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# 在Unity 3D中保留我的十进制计算_C#_Unity3d_Decimal_Calculation - Fatal编程技术网

C# 在Unity 3D中保留我的十进制计算

C# 在Unity 3D中保留我的十进制计算,c#,unity3d,decimal,calculation,C#,Unity3d,Decimal,Calculation,每当我在计算中添加一个小数点(0.052),它默认为0 如何保留代码的十进制计算 例如,int-MWG=(int)(MD*常数);该常数默认为0,而不是0.052 public class input field: MonoBehaviour { private float constant = 0.052f; public void CasingPressure() { theTVD = inputField.GetComponent<Text>

每当我在计算中添加一个小数点(0.052),它默认为0

如何保留代码的十进制计算

例如,int-MWG=(int)(MD*常数);该常数默认为0,而不是0.052

public class input field: MonoBehaviour
{
    private float constant = 0.052f;

public void CasingPressure()

    {

        theTVD = inputField.GetComponent<Text>().text;
        theGas = inputFieldGas.GetComponent<Text>().text;
        theMudWeight = inputFieldMudWeight.GetComponent<Text>().text;
        theInfluxLength = inputFieldLengthofInflux.GetComponent<Text>().text;
        theFormationPressure = inputFieldFormationPressure.GetComponent<Text>().text;

        int TVD = Convert.ToInt32(theTVD);
        int KD = Convert.ToInt32(theGas);
        int MW = Convert.ToInt32(theMudWeight);
        int Influx = Convert.ToInt32(theInfluxLength);
        int FP = Convert.ToInt32(theFormationPressure);

        int C = TVD + MW;

        int HP = TVD * MW * (int)(constant);
        int SDIPP = FP - HP ;
        int MD = MW - KD;
        int MWG = (int)(MD * constant);
        int LMWG = MWG * Influx;
        int SICPP = LMWG + SDIPP;
        textDisplayCP.GetComponent<Text>().text = " " + SICPP + " psi";

    }
公共类输入字段:MonoBehavior
{
私人浮动常数=0.052f;
公共空隙套管压力()
{
theTVD=inputField.GetComponent().text;
theGas=inputFieldGas.GetComponent().text;
mudwight=InputFieldMudwight.GetComponent().text;
influxLength=inputFieldLengthofInflux.GetComponent().text;
FormationPressure=inputFieldFormationPressure.GetComponent().text;
int TVD=转换为NT32(theTVD);
int KD=转换为NT32(气体);
int MW=转换为NT32(最大重量);
int Influx=转换为Int32(influxLength);
int FP=转换为Int32(地层压力);
INTC=TVD+MW;
int HP=TVD*MW*(int)(常数);
int-SDIPP=FP-HP;
int-MD=MW-KD;
int MWG=(int)(MD*常数);
int LMWG=MWG*流入量;
int SICPP=LMWG+SDIPP;
textDisplayCP.GetComponent().text=”“+SICPP+“psi”;
}

这并不是说
常量的值默认为零。这是因为您将强制转换为
int
。如果希望保留小数,则必须通过强制转换具有小数的类型来使类型兼容。将
int
强制转换为
float
,而不是相反。int不支持“小数”或浮点值,如果要保留小数,请不要使用int。存储结果的类型也需要是支持小数的类型。在这种情况下,
float
——
float MWG=(float)MD*constant;
将浮点数强制转换为整数类型将向零舍入。在这种情况下,将
常量强制转换为
int
将其舍入为
0
@coder
myVariableName.ToString(“N2”)
表示要显示的小数点数量的数字。如果只想显示整数,请使用
N0