加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#中格式化大数字

发布时间:2020-12-15 08:16:54 所属栏目:百科 来源:网络整理
导读:我正在使用Unity制作一个“增量游戏”,也称为“空闲游戏”,我正在尝试格式化大数字.例如,当黄金达到1000或更高时,它将显示为黄金:1k而不是黄金:1000. using UnityEngine;using System.Collections;public class Click : MonoBehaviour { public UnityEngin
我正在使用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 );
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读