unity實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
本文實(shí)例為大家分享了unity實(shí)現(xiàn)簡(jiǎn)單計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
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;//避免減為零后的第二次起不能為負(fù)
private bool? isPlusOrDecrease, countType;//點(diǎn)擊加減符號(hào),點(diǎn)擊等號(hào)
public Button PointButton;
private int count;//限制最大輸入數(shù)
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)//避免第一次點(diǎn)擊加號(hào)時(shí)沒(méi)反應(yīng)
{
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)//避免第一次點(diǎn)擊減號(hào)時(shí)沒(méi)反應(yīng)
{
TotalCount();
}
CountNum();
}
else
{
count = 0;
PointButtonController(true);
totalPrice = 0;
spendPrice.Clear();
SpendText.DOText(rmbSymbol, 0);
isFirstDecrease = true;
}
}
break;
case 13://點(diǎn)
PointButtonController(false);
spendPrice.Append(".");
SpendText.DOText(rmbSymbol + spendPrice.ToString(), 0.1f);
break;
case 14://等號(hào)
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(""))//去除開(kāi)始的無(wú)效零
{
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;
}
}
//將科學(xué)計(jì)數(shù)法轉(zhuǎn)化為普通數(shù)字
private Decimal ChangeDataToD(string strData)
{
Decimal dData = 0.0M;
if (strData.Contains("E"))
{
dData = Decimal.Parse(strData, System.Globalization.NumberStyles.Float);
}
return dData;
}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
RegexOptions.IgnoreCase正則表達(dá)式替換,忽略大小寫
RegexOptions.IgnoreCase正則表達(dá)式替換,忽略大小寫,需要的朋友可以參考一下2013-03-03
深入分析C#中WinForm控件之Dock順序調(diào)整的詳解
本篇文章是對(duì)C#中WinForm控件之Dock順序調(diào)整進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
c#基于Redis實(shí)現(xiàn)輕量級(jí)消息組件的步驟
這篇文章主要介紹了c#基于Redis實(shí)現(xiàn)輕量級(jí)消息組件的步驟,幫助大家更好的理解和學(xué)習(xí)使用c#進(jìn)行開(kāi)發(fā),感興趣的朋友可以了解下2021-05-05
C# Char結(jié)構(gòu)中IsLetterOrDigit(Char)的方法詳解
這篇文章給大家介紹了C#的Char 結(jié)構(gòu)的IsLetterOrDigit(Char)的方法,并通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02
C#.NET中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中
這篇文章主要給大家介紹C#.net中如何批量插入大量數(shù)據(jù)到數(shù)據(jù)庫(kù)中,本文涉及到C#.net中批量插入數(shù)據(jù)到數(shù)據(jù)庫(kù)中方面的內(nèi)容,對(duì)C#.net批量插入數(shù)據(jù)到數(shù)據(jù)庫(kù)中感興趣的朋友可以參考下本篇文章2015-10-10
Netcore?Webapi返回?cái)?shù)據(jù)的三種方式示例
這篇文章主要為大家介紹了Netcore?Webapi返回?cái)?shù)據(jù)的三種方式示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
c# Newtonsoft.Json 常用方法總結(jié)
這篇文章主要介紹了c# Newtonsoft.Json 常用方法的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02

