unity实现简单计算器

 更新时间:2021年08月08日 15:06:59   作者:BackyStone  
这篇文章主要为大家详细介绍了unity实现简单计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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;
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • C#基础知识之base关键字介绍

    C#基础知识之base关键字介绍

    本文主要介绍base关键字的使用方法,base关键字可以调用基类重写的方法,可以调用基类的构造方法,还可以在EntityFramework中使用,下面一一介绍。
    2016-04-04
  • unity实现动态排行榜

    unity实现动态排行榜

    这篇文章主要为大家详细介绍了unity实现动态排行榜,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • C#调用usb摄像头的实现方法

    C#调用usb摄像头的实现方法

    这篇文章主要介绍了C#调用usb摄像头的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • c#利用Grahics进行图片裁剪

    c#利用Grahics进行图片裁剪

    这两天做了一个图片对比工具,里面要处理两张大的图片,所以要对图片先进行裁剪,下面看看我的方法吧
    2013-12-12
  • C#操作注册表之Registry类

    C#操作注册表之Registry类

    这篇文章介绍了C#操作注册表之Registry类,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • C#中把字符串String转换为整型Int的小例子

    C#中把字符串String转换为整型Int的小例子

    这篇文章主要介绍了C#中把字符串String转换为整型Int的小例子,本文使用TryParse方法实现转换,需要的朋友可以参考下
    2014-08-08
  • winform简单缓存类实例

    winform简单缓存类实例

    这篇文章主要介绍了winform简单缓存类,涉及C#缓存使用技巧,非常简单实用,需要的朋友可以参考下
    2015-09-09
  • Jquery+Ajax+Json+存储过程实现高效分页

    Jquery+Ajax+Json+存储过程实现高效分页

    这篇文章主要介绍Jquery+Ajax+Json+存储过程实现分页,需要的朋友可以参考下
    2015-08-08
  • 关于C# Math 处理奇进偶不进的实现代码

    关于C# Math 处理奇进偶不进的实现代码

    下面小编就为大家带来一篇关于C# Math 处理奇进偶不进的实现代码。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-05-05
  • 一文带你了解C#中抽象方法与虚方法的区别

    一文带你了解C#中抽象方法与虚方法的区别

    这篇文章主要通过简单的示例为大家详细介绍一下C#中抽象方法与虚方法的区别,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-03-03

最新评论