在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 ); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- c# – 处理方法参数的null属性的最佳方法
- ruby-on-rails – 如何使用Redcarpet for Rails在Markdown中
- Swift中的多类型约束
- c – 为(auto i:unordered_map)保证每次都有相同的顺序?
- c# – 我可以将剪贴板作为命令参数发送吗?
- quick-cocos2d-x如何在mac下编译安卓版本
- 正则匹配这样两个字符串a'bc'd,abcd
- Solr 配置文件之core.properties、solr.xml和Config Sets
- 行为驱动开发(BDD)和验收测试驱动开发(ATDD)之间有什么区别
- asset-pipeline – 没有路由匹配Rails中的资产/图像