微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在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 );
}

原文地址:https://www.jb51.cc/csharp/239215.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐