在C#中格式化大数字

前端之家收集整理的这篇文章主要介绍了在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(“#,#”);来自,但他们都没有工作.@H_502_5@

解决方法

我在我的项目中使用此方法,您也可以使用.也许有更好的方法,我不知道.
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";
        }
    }

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

void Update()
{
     KMBMaker( GoldDisplay.text,gold );
}
原文链接:https://www.f2er.com/csharp/239215.html

猜你在找的C#相关文章