[C#Unity]1E+;07显示为10mln
我还在学习C#,我正在制作简单的点击游戏。我的问题是,如果一些数字达到10000,它们开始显示为1E+07(例如)。重点是,我希望它显示为10mln和更高(bln等)。每次我试着这么做,我都会犯很多错误。你能给我一些建议我怎么做[C#Unity]1E+;07显示为10mln,c#,unity3d,C#,Unity3d,我还在学习C#,我正在制作简单的点击游戏。我的问题是,如果一些数字达到10000,它们开始显示为1E+07(例如)。重点是,我希望它显示为10mln和更高(bln等)。每次我试着这么做,我都会犯很多错误。你能给我一些建议我怎么做 using UnityEngine; using System.Collections; using System; public class ping : MonoBehaviour { public UnityEngine.UI.Text gpc; public
using UnityEngine;
using System.Collections;
using System;
public class ping : MonoBehaviour {
public UnityEngine.UI.Text gpc;
public UnityEngine.UI.Text GoldDisplay;
public static float gold = 0.00f;
public static long toInt64()
{
return Convert.ToInt64(gold);
}
public int goldperclick = 1;
void Update(){
GoldDisplay.text = "Ping: " + gold;
gpc.text = goldperclick + "ping/click";
}
public void Clicked(){
gold += goldperclick;
}
}
也许您应该实现自己的类型,比如Gold,并为其重写ToString()方法
public class Program
{
public static void Main()
{
Console.WriteLine(new Gold(10000)); //10000
Console.WriteLine(new Gold(100000)); //100000
Console.WriteLine(new Gold(1000000)); //1 mln.
Console.WriteLine(new Gold(10000000)); //10 mln.
Console.WriteLine(new Gold(100000000)); //100 mln.
Console.WriteLine(new Gold(1000000000)); //1 bln.
Console.WriteLine(new Gold(1500000000)); //1.5 bln.
}
}
public class Gold
{
public decimal Value {get;set;}
public Gold(decimal value)
{
Value = value;
}
public override string ToString()
{
if(Value>=1000000000) return $"{Value/1000000000} bln.";
if(Value>=1000000) return $"{Value/1000000} mln.";
return Value.ToString();
}
}
此外,您应该根据最大值和精度明智地选择数字类型
您可以找到有关大数格式的更多详细信息。如果您只处理整数,可能最好使用
长的而不是浮点数
带符号的long
或(int64)最多可容纳9223372036854775807个值,并将显示这些值中的每一个,而浮点仅提供7位精度-因此,如果值为1234567901
,您的浮点将仅显示1234568000
,这可能不是您所期望的(这也可以通过打印出float.MaxValue.ToString(“0”)
看到,它在填充零之前只显示七个有效数字)
如果您需要使用小数,另一种选择是使用允许15-16位精度的double
。很好的答案。我只想补充一点,这个类可以很容易地成为格式化数字
,并且可以用于处理大数字的点击游戏中的任何事情。谢谢!我会尝试使用它。我想要d这样做,但当我将gold form flaot更改为long时,我遇到了一个错误:无法将类型“float”隐式转换为“long”。因此,我可能做错了。@Bajor您需要将对ping.gold
的任何引用更新为long
s。这里仍然存在此错误:公共类ItemManager:MonoBehavior{public UnityEngine.UI.Text itemInfo;public ping ping;public long cost;public int tickValue;public long count;public string itemName;private long baseCost;void Start(){baseCost=cost;}void Update(){itemInfo.Text=itemName+“\nKoszt:”+cost+“\nPing:“+tickValue+”/s”}public void PurchasedItem(){if(ping.gold>=cost){ping.gold-=cost;count+=1;cost=Mathf.Round(baseCost*Mathf.Pow(1.15f,count));//Mathf.Round
仍然返回一个浮点数,而不是长点数,因此您仍然会在那里得到该错误。