https://blog.csdn.net/QuVi_God/article/details/83382628

根据这篇游戏金币管理类文章,所改写的Java版本的游戏金币管理类。

---------------------------------

金币单位枚举类:

public enum GoldUnit {//自定义的金币单位类型N(0), K(1), M(2), B(3), T(4),aa(5),ab(6),ac(7),ad(8),ae(9),af(10),ag(11),ah(12),ai(13),aj(14),ak(15),al(16),am(17),an(18),ao(19),ap(20),aq(21),ar(22),as(23),at(24),au(25),av(26),aw(27),ax(28),ay(29),az(30),;private int value;private GoldUnit(int value){this.value = value;}public int getValue(){return value;}public static GoldUnit parse(int value){GoldUnit[] states = GoldUnit.values();for (GoldUnit s : states){if (s.getValue() == value)return s;}return N;}
}

金币单位管理实现类:

public class GoldNum {/*** 金币+单位*/private String goldCountText;/*** 单位枚举*/private Integer goldUnit;/*** 金币金额*/private Double goldValue;public String GoldCountText;public String value;// 构造函数public GoldNum(String value) {value = value.trim();// 删除字符串首部和尾部的空格boolean isSingleDigit = true;//遍历金币单位枚举for (int i = 30; i >= 0; i--) {if (value.endsWith(GoldUnit.parse(i).toString())) {value = value.replace(GoldUnit.parse(i).toString(), "");isSingleDigit = false;goldUnit = i;goldValue = Double.parseDouble(value);break;}}//没有单位if (isSingleDigit) {goldUnit = 0;goldValue = Double.parseDouble(value);}//值大于500  进一位while (goldValue > 500 && goldUnit < 30) {goldUnit++;goldValue /= 1000;}//值大于0 小于 1 且 拥有金币单位  退一位while (goldValue > 0 && goldValue < 1 && goldUnit > 0) {goldUnit--;goldValue *= 1000;}}// 构造函数public GoldNum(Double value) {goldUnit = 0;goldValue = (double) Math.round(value);while (goldValue > 500 && goldUnit < 30) {goldUnit++;goldValue /= 1000;}while (goldValue > 0 && goldValue < 1 && goldUnit > 0) {goldUnit--;goldValue *= 1000;}}public String getGoldCountText() {String p="";if (goldUnit == 0) {goldCountText = Math.round(goldValue) + "";} else {BigDecimal bg = new BigDecimal(goldValue);float tempValue = bg.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();if (Math.round(tempValue) - tempValue == 0) {p=String.valueOf(Math.round(tempValue));}else{//保留小数点后两位DecimalFormat decimalFormat = new DecimalFormat(".00");p = decimalFormat.format(tempValue);}goldCountText = p + (GoldUnit.parse(goldUnit)).toString();}return goldCountText;}public void setGoldCountText(String value) {if (value != null) {if (!value.equals(this.value)) {this.value = value;}} else {if (value != this.value) {this.value = value;}}goldCountText = value;}public Integer getGoldUnit() {return goldUnit;}public void setGoldUnit(Integer goldUnit) {this.goldUnit = goldUnit;}public Double getGoldValue() {return goldValue;}public void setGoldValue(Double goldValue) {this.goldValue = goldValue;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}public static GoldNum GoldNum(String value) {GoldNum goldNum = null;int length = value.length();if (length <= 3) {goldNum = new GoldNum(value);}return goldNum;}/*** 加法* * @param A* @param B* @return*/public static GoldNum add(GoldNum A, GoldNum B) {int limit = 4;int tempUnit = 0;Double tempValue = 0.0;if (A.goldUnit == B.goldUnit) {tempUnit = A.goldUnit;tempValue = A.goldValue + B.goldValue;} else if (A.goldUnit > B.goldUnit) {if (A.goldUnit - B.goldUnit <= limit) {tempUnit = A.goldUnit;tempValue = (Double) (A.goldValue + B.goldValue / Math.pow(1000, A.goldUnit - B.goldUnit));} else if (A.goldUnit - B.goldUnit > limit) {tempUnit = A.goldUnit;tempValue = A.goldValue;}} else if (A.goldUnit < B.goldUnit) {if (B.goldUnit - A.goldUnit <= limit) {tempUnit = B.goldUnit;tempValue = (Double) (A.goldValue / Math.pow(1000, B.goldUnit - A.goldUnit) + B.goldValue);} else if (B.goldUnit - A.goldUnit > limit) {tempUnit = B.goldUnit;tempValue = B.goldValue;}}GoldNum result = new GoldNum(tempValue + (GoldUnit.parse(tempUnit)).toString());return result;}/*** 减法* * @param A* @param B* @return*/public static GoldNum sub(GoldNum A, GoldNum B) {int limit = 4;int tempUnit = 0;Double tempValue = 0.0;if (A.goldUnit == B.goldUnit) {tempUnit = A.goldUnit;tempValue = A.goldValue - B.goldValue;if(tempValue==0){tempUnit=0;}} else if (A.goldUnit > B.goldUnit) {if (A.goldUnit - B.goldUnit <= limit) {tempUnit = A.goldUnit;tempValue = (Double) (A.goldValue - B.goldValue / Math.pow(1000, A.goldUnit - B.goldUnit));} else if (A.goldUnit - B.goldUnit > limit) {tempUnit = A.goldUnit;tempValue = A.goldValue;}} else if (A.goldUnit < B.goldUnit) {if (B.goldUnit - A.goldUnit <= limit) {tempUnit = B.goldUnit;tempValue = (Double) (A.goldValue / Math.pow(1000, B.goldUnit - A.goldUnit) - B.goldValue);} else if (B.goldUnit - A.goldUnit > limit) {tempUnit = B.goldUnit;tempValue = -B.goldValue;}}GoldNum result = new GoldNum(tempValue + (GoldUnit.parse(tempUnit)).toString());return result;}/*** 乘法* * @param A* @param B* @return*/public static GoldNum mul(GoldNum A, float B) {int tempUnit = A.goldUnit;Double tempValue = A.goldValue * B;GoldNum result = new GoldNum(tempValue + (GoldUnit.parse(tempUnit)).toString());return result;}/*** 乘法* * @param A* @param B* @return*/public static GoldNum mul(GoldNum A, GoldNum B) {int tempUnit = A.goldUnit + B.goldUnit;Double tempValue = A.goldValue * B.goldValue;GoldNum result = new GoldNum(tempValue + (GoldUnit.parse(tempUnit)).toString());return result;}/*** 除法* * @param A* @param B* @return*/public static GoldNum div(GoldNum A, float B) {int tempUnit = A.goldUnit;Double tempValue = A.goldValue / B;GoldNum result = new GoldNum(tempValue + (GoldUnit.parse(tempUnit)).toString());return result;}/*** 大于* * @param A* @param B* @return*/public static boolean than(GoldNum A, GoldNum B) {GoldNum result = GoldNum.sub(A, B);return result.getGoldCountText().startsWith("-");}/*** 小于* * @param A* @param B* @return*/public static boolean less(GoldNum A, GoldNum B) {GoldNum result = GoldNum.sub(A, B);return result.getGoldCountText().startsWith("-");}/*** 大于并且等于* * @param A* @param B* @return*/public static boolean thanAndEqual(GoldNum A, GoldNum B) {GoldNum result = GoldNum.sub(A, B);return !result.getGoldCountText().startsWith("-") || result.getGoldCountText() == "0";}/*** 小于并且等于* * @param A* @param B* @return*/public static boolean lessAndEqual(GoldNum A, GoldNum B) {GoldNum result = GoldNum.sub(A, B);return result.getGoldCountText().startsWith("-") || result.GoldCountText == "0";}/*** 随机数* * @param A* @param B* @return*/public static GoldNum DoubleRandom(GoldNum A, GoldNum B) {int limit = 4;int tempUnit = 0;Double tempValue = 0.0;GoldNum result = null;ThreadLocalRandom random = ThreadLocalRandom.current();if (A.goldUnit == B.goldUnit) {tempUnit = A.goldUnit;if(A.getGoldValue().equals(B.getGoldValue())){tempValue=A.getGoldValue();}else{tempValue = random.nextDouble(A.getGoldValue(), B.getGoldValue());}result = new GoldNum(tempValue + (GoldUnit.parse(A.getGoldUnit())).toString());} else if (B.goldUnit > A.goldUnit) {if (B.goldUnit - A.goldUnit <= limit) {tempValue = (Double) (B.goldValue * Math.pow(1000, B.goldUnit - A.goldUnit));tempValue = random.nextDouble(A.getGoldValue(), tempValue);if (tempValue > 1000) {result = new GoldNum(tempValue + (GoldUnit.parse(B.getGoldUnit()-1)).toString());} else {result = new GoldNum(tempValue + (GoldUnit.parse(A.getGoldUnit())).toString());}} else if (A.goldUnit - B.goldUnit > limit) {tempUnit = A.goldUnit;tempValue = A.goldValue;}}return result;}@Overridepublic String toString() {return "GoldNum [goldCountText=" + goldCountText + ", goldUnit=" + goldUnit + ", goldValue=" + goldValue+ ", GoldCountText=" + GoldCountText + ", value=" + value + "]";}
}

游戏自定义金币单位换算管理类相关推荐

  1. 游戏金币单位换算管理类

    using System.Collections; using System.Collections.Generic; using UnityEngine; //金币单位 public enum Go ...

  2. 计算机专业可以考事业单位a类吗,事业单位综合管理类a类考什么

    事业单位综合管理类a类考<职业能力倾向测验>(包括常识判断.言语理解与表达.数量关系.判断推理和资料分析等部分):<综合应用能力(A类)>(主要测查应试人员的管理角色意识.分析 ...

  3. 数字、时间等单位换算工具类收集

    将字节数转换成KB或者MB public static String bytes2kb(long bytes) {BigDecimal filesize = new BigDecimal(bytes) ...

  4. Qt重定向QDebug,自定义一个简易的日志管理类

    0.前言 相对于第三方的日志库,在 Qt 中使用 QDebug 打印更便捷,有时候也需要对 QDebug 输出进行重定向,如写入文件等. 在 Qt4 中使用 qInstallMsgHandler 函数 ...

  5. 个人永久性免费-Excel催化剂功能第35波-Excel版最全单位换算,从此不用到处百度找答案...

    全球化的今天,相信我们经常可以有机会接触到外国的产品,同时我们也有许多产品出口到外国,国与国之间的度量单位不一,经常需要做一些转换运算,一般网页提供这样的转换,但没有什么比在Excel上计算来得更为方 ...

  6. js距离单位换算_JS数据容量单位转换(kb,mb,gb,tb)

    JS代码如下: var size = '8164674'; function bytesToSize(bytes) { if (bytes === 0) return '0 B'; var k = 1 ...

  7. Flutter 适配android/iOS设备的单位换算

    参考 : flutter 屏幕适配方案自定义单位 前几天获取了Flutter 屏幕的宽高, 基于这个, 我们再搞一下适配的问题, 依旧是基于主流设计的机型 iPhone6s 的尺寸 pt:375 / ...

  8. 2019年全国硕士研究生入学统一考试管理类专业学位联考逻辑试题

    2019年一月联考逻辑真题 三.逻辑推理:第 26-55 小题,每小题 2 分,共 60 分.下列每题给出的 A.B.C.D. E 五个选项中,只有一项是符合试题要求的.请在答题卡上将所选项的字母涂黑 ...

  9. 万航单位换算器 V1.0 绿色版

    软件名称: 万航单位换算器 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win 32位/64位 软件大小: 347KB 图片预览: 软件简介: 万航单位换算器是一个可以随意转换单位的绿色软 ...

最新文章

  1. python学到什么程度可以做兼职-Python学到什么程度才可以去找工作?掌握这4点足够了!...
  2. Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理
  3. CSS SANS – 神奇!使用 CSS3 创建的字体
  4. ALV中下拉列表列的实现
  5. Python3字符串的编码
  6. c语言中热河输入空格,承德市2020年(春秋版)小学英语六年级上册期中考试模拟试卷(1)C卷...
  7. windows下flv视频网站进度条随意拖放[转]
  8. Android Studio目录结构分析
  9. 第六节:深入研究Task实例方法ContinueWith的参数TaskContinuationOptions
  10. 《DirectX 9.0 3D游戏开发编程基础》 第一章 初始化Direct3D 读书笔记
  11. java 多线程取一条记录_java多线程从队列中取出数据执行
  12. 安全验证框架使用笔记001---Shiro简介
  13. C#------引用System.Data.Entity后DbContext依然无法继承解决方法
  14. hive分析函数取最新_Hive的分析函数的学习
  15. php删除session中的值,php如何删除session中数据
  16. 铲除浏览器右键菜单中的QQ相关项目(转)
  17. win10 请求操作需要提升解决方案
  18. Redis过期键删除策略
  19. 会玩,也是一种积极的生活态度
  20. Win10笔记本WIFI的标志突然变成了一个地球的解决方案(二)

热门文章

  1. i春秋 429-线上赛题(一)Writeup
  2. Unsupervised Learning: Deep Auto-encoder
  3. 网站内嵌编辑器ace
  4. 关于Android证书MD5获取
  5. linux下执行gauss的脚本文件,求脚本:同时向n个nodes提交并执行当前目录下所有Gaussian输入文件...
  6. 微信为什么打不开html,微信为什么打不开?解决微信打不开图文教程
  7. WEB端显示摄像头实时图像数据
  8. 一文归纳Ai调参炼丹之法
  9. 数据结构和算法(二):摘要算法之SHA和MD5
  10. 新红楼造型雷死人 有才网友改编老版经典台词