C# 在Unity 3D中保留我的十进制计算
每当我在计算中添加一个小数点(0.052),它默认为0 如何保留代码的十进制计算 例如,int-MWG=(int)(MD*常数);该常数默认为0,而不是0.052C# 在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>
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
@codermyVariableName.ToString(“N2”)
表示要显示的小数点数量的数字。如果只想显示整数,请使用N0