在C#中格式化大数字

我正在使用Unity制作一个“增量游戏”,也称为“空闲游戏”,我正在尝试格式化大数字.例如,当黄金达到1000或更高时,它将显示为黄金:1k而不是黄金:1000.
using UnityEngine;
using System.Collections;

public class Click : MonoBehavIoUr {

    public UnityEngine.UI.Text Golddisplay;
    public UnityEngine.UI.Text GPC;
    public double gold = 0.0;
    public int gpc = 1;

    void Update(){
        Golddisplay.text = "Gold: " +  gold.ToString ("#,#");
        //Following is attempt at changing 10,000,000 to 10.0M
        if (gold >= 10000000) {
        Golddisplay.text = "Gold: " + gold.ToString ("#,#M");
        }
        GPC.text = "GPC: " + gpc;
    }

    public void Clicked(){
            gold += gpc;
    }
}

我在网上搜索时尝试过其他例子,这就是gold.ToString(“#,#”);来自,但他们都没有工作.

解决方法

我在我的项目中使用此方法,您也可以使用.也许有更好的方法,我不知道.
public void KMBMaker( Text txt,double num )
    {
        if( num < 1000 )
        {
            double numStr = num;
            txt.text = numStr.ToString() + "";
        }
        else if( num < 1000000 )
        {
            double numStr = num/1000;
            txt.text = numStr.ToString() + "K";
        }
        else if( num < 1000000000 )
        {
            double numStr = num/1000000;
            txt.text = numStr.ToString() + "M";
        }
        else
        {
            double numStr = num/1000000000;
            txt.text = numStr.ToString() + "B";
        }
    }

并在此更新中使用此方法.

void Update()
{
     KMBMaker( Golddisplay.text,gold );
}

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...