unity实现简单计算器
程序员文章站
2022-06-09 19:15:52
本文实例为大家分享了unity实现简单计算器的具体代码,供大家参考,具体内容如下using system.text;using unityengine;using unityengine.ui;usi...
本文实例为大家分享了unity实现简单计算器的具体代码,供大家参考,具体内容如下
using system.text; using unityengine; using unityengine.ui; using dg.tweening; using system; public class calculator : monobehaviour { public text spendtext; private stringbuilder spendprice;//初始金额 private string rmbsymbol; private float totalprice, spendprices;//总和,初始金额 private bool isfirstdecrease;//避免减为零后的第二次起不能为负 private bool? isplusordecrease, counttype;//点击加减符号,点击等号 public button pointbutton; private int count;//限制最大输入数 private void start() { spendprice = new stringbuilder(); totalprice = 0; spendprices = 0; rmbsymbol = "<size='50'>¥</size> "; isplusordecrease = null;//true为加,false为减 counttype = null;//true为加,false为减 isfirstdecrease = true; count = 0; } public void pointbuttoncontroller(bool type) { pointbutton.interactable = type; } public void inputnumber(int num) { //按钮 switch (num) { case 0: if (count < 11) { count++; spendprice.append("0"); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); } break; case 1: if (count < 11) { count++; spendprice.append("1"); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); } break; case 2: if (count < 11) { count++; spendprice.append("2"); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); } break; case 3: if (count < 11) { count++; spendprice.append("3"); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); } break; case 4: if (count < 11) { count++; spendprice.append("4"); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); } break; case 5: if (count < 11) { count++; spendprice.append("5"); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); } break; case 6: if (count < 11) { count++; spendprice.append("6"); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); } break; case 7: if (count < 11) { count++; spendprice.append("7"); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); } break; case 8: if (count < 11) { count++; spendprice.append("8"); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); } break; case 9: if (count < 11) { count++; spendprice.append("9"); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); } break; case 10: if (count < 11) { count += 2; spendprice.append("00"); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); } break; case 11://加 isplusordecrease = true; counttype = true; count = 0; if (!spendprice.tostring().equals("")) { if ((totalprice + float.parse(spendprice.tostring()) < 99999999999) && !totalprice.tostring().contains("e")) { pointbuttoncontroller(true); if (totalprice != 0)//避免第一次点击加号时没反应 { totalcount(); } countnum(); } else { count = 0; pointbuttoncontroller(true); totalprice = 0; spendprice.clear(); spendtext.dotext(rmbsymbol, 0); isfirstdecrease = true; } } break; case 12://减 isplusordecrease = false; counttype = false; count = 0; if (!spendprice.tostring().equals("")) { if ((totalprice + float.parse(spendprice.tostring()) < 99999999999) && !totalprice.tostring().contains("e")) { pointbuttoncontroller(true); if (totalprice != 0)//避免第一次点击减号时没反应 { totalcount(); } countnum(); } else { count = 0; pointbuttoncontroller(true); totalprice = 0; spendprice.clear(); spendtext.dotext(rmbsymbol, 0); isfirstdecrease = true; } } break; case 13://点 pointbuttoncontroller(false); spendprice.append("."); spendtext.dotext(rmbsymbol + spendprice.tostring(), 0.1f); break; case 14://等号 count = 0; if (!spendprice.tostring().equals("")) { if ((totalprice + float.parse(spendprice.tostring()) < 9999999999) && !totalprice.tostring().contains("e")) { pointbuttoncontroller(true); totalcount(); } else { count = 0; pointbuttoncontroller(true); totalprice = 0; spendprice.clear(); spendtext.dotext(rmbsymbol, 0); isfirstdecrease = true; } } break; case 15://清零 count = 0; pointbuttoncontroller(true); totalprice = 0; spendprice.clear(); spendtext.dotext(rmbsymbol, 0); isfirstdecrease = true; break; default: break; } } public void countnum() { if (spendprice.tostring().startswith("0") || spendprice.tostring().equals(""))//去除开始的无效零 { if (spendprice.tostring().trimstart('0') == "" || spendprice.tostring().trimstart('0').trimend('0') == ".")//0000,00.00,0.,.0 { spendprices = 0; } else { spendprices = float.parse((float.parse(spendprice.tostring().trimstart('0'))).tostring("f2")); } } else { spendprices = float.parse((float.parse(spendprice.tostring())).tostring("f2")); } if (isplusordecrease == true && totalprice != 0 && spendprices != 0) { totalprice += spendprices; spendprice.clear(); spendtext.dotext(rmbsymbol + totalprice.tostring(), 0.1f); isplusordecrease = null; } else if (isplusordecrease == true && totalprice == 0 && spendprices != 0 && spendprices != 0) { totalprice = spendprices; spendprice.clear(); } if (isplusordecrease == false && totalprice == 0 && spendprices != 0 && isfirstdecrease) { totalprice = spendprices; spendprice.clear(); isfirstdecrease = false; } else if (isplusordecrease == false && spendprices != 0) { totalprice -= spendprices; spendprice.clear(); spendtext.dotext(rmbsymbol + totalprice.tostring(), 0.1f); isplusordecrease = null; } } public void totalcount() { if (spendprice.tostring().startswith("0") || spendprice.tostring().equals("")) { if (spendprice.tostring().trimstart('0') == "" || spendprice.tostring().trimstart('0').trimend('0') == ".") { spendprices = 0; } else { spendprices = float.parse((float.parse(spendprice.tostring().trimstart('0'))).tostring("f2")); } } else { spendprices = float.parse((float.parse(spendprice.tostring())).tostring("f2")); } if (spendprices != 0) { if (counttype == true) { totalprice += spendprices; } else if (counttype == false) { totalprice -= spendprices; } spendprice.clear(); spendtext.dotext(rmbsymbol + totalprice.tostring(), 0.1f); counttype = null; } } //将科学计数法转化为普通数字 private decimal changedatatod(string strdata) { decimal ddata = 0.0m; if (strdata.contains("e")) { ddata = decimal.parse(strdata, system.globalization.numberstyles.float); } return ddata; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 轻松入门正则表达式之非贪婪匹配篇详解
下一篇: 2019前端面试题总结一波