简体   繁体   English

C# - 大数到带单位的数(10000 到 10K)

[英]C# - Large number to number with unit (10000 to 10K)

So I'm making a clicker game and I got it working but my code is very repetitive.所以我正在制作一个唱首歌游戏并且我让它工作但我的代码非常重复。 I want it to be less repetitive but I don't really know how to do that.我希望它不那么重复,但我真的不知道该怎么做。 This is what I currently have, you'll notice its not very clean, all I want to do is simplify the void Update part.这就是我目前拥有的,您会注意到它不是很干净,我想做的就是简化 void Update 部分。 Cause with the way I'm doing it now I'm giving myself a lot of work and unnecessary code.因为我现在这样做的方式给自己带来了很多工作和不必要的代码。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public long coins; // Coin value
    private double currentCoins; // Coin value above 10000 in Unit
    private long addition; // Addition per click
    public int upgradeLevel; // Upgrades bought
    private int multiplier; // Multiplier based on upgrades bought
    public GameObject coinText;
    public GameObject coinUnit;

    void Start ()
    {
        coins = 0; // Start capital
        upgradeLevel = 1; // Start level
        multiplier = 2; // First multiplier (active when upgrade level 25 |>)
    }

    void Update ()
    {
        if (coins.ToString().Length < 4)
        {
            coinUnit.GetComponent<Text>().text = "";
            coinText.GetComponent<Text>().text = coins.ToString();
        } else {
            if (coins.ToString().Length < 7)
            {
                currentCoins = coins;
                coinUnit.GetComponent<Text>().text = "K";
                currentCoins = currentCoins / 1000;
                coinText.GetComponent<Text>().text = Math.Round(currentCoins, 2).ToString();
            } else {
                if (coins.ToString().Length < 10)
                {
                    currentCoins = coins;
                    coinUnit.GetComponent<Text>().text = "M";
                    currentCoins = currentCoins / 1000000;
                    coinText.GetComponent<Text>().text = Math.Round(currentCoins, 2).ToString();
                } else {
                    if (coins.ToString().Length < 13)
                    {
                        currentCoins = coins;
                        coinUnit.GetComponent<Text>().text = "B";
                        currentCoins = currentCoins / 1000000000;
                        coinText.GetComponent<Text>().text = Math.Round(currentCoins, 2).ToString();
                    } else {
                        currentCoins = coins;
                        coinUnit.GetComponent<Text>().text = "T";
                        currentCoins = currentCoins / 1000000000000;
                        coinText.GetComponent<Text>().text = Math.Round(currentCoins, 2).ToString();
                    }
                }
            }
        }
    }

    public void OnClick()
    {
        if (upgradeLevel < 25)
        {
            addition = 1 * upgradeLevel;
            coins = coins + addition;
        } else {
            multiplier = 2 * (upgradeLevel / 25);
            addition = (1 * upgradeLevel) * multiplier;
            coins = coins + addition;
        }
    }
}

Thanks in advance提前致谢

EDIT: would it be possible to assign a value such as K based on how many times 1000 fits in coins (coins = 1000; coins / 1000 = 1, so it would put K)编辑:是否可以根据 1000 适合硬币的次数来分配诸如 K 之类的值(硬币 = 1000;硬币 / 1000 = 1,所以它会放 K)

This is a little cleaner.这有点清洁。

var coinDescription = new CoinDescriptor(coins);
coinUnit.GetComponent<Text>().text = coinDescription.Label;
coinText.GetComponent<Text>().text = coinDescription.CoinText;
...
public class CoinDescriptor { 
    public CoinDescriptor(long  coinCount)
    {
        TotalCoinCount = coinCount;
        decimal convertedCoinCount;
        if (coinCount < 1000) {
            Label ="";
            convertedCoinCount = TotalCoinCount;
        }
        else if (coinCount < 1000000000)
        {
            Label = "M";
            convertedCoinCount = Math.Round(TotalCoinCount / 1000M, 2);
        }
        else if (coinCount < 1000000000000)
        {
            Label = "B";
            convertedCoinCount = Math.Round(TotalCoinCount / 1000000000M, 2);
        }
        else
        {
            Label = "T";
            convertedCoinCount = Math.Round(TotalCoinCount / 1000000000000M, 2);
        }
        CoinText = String.Format("{0:n0}", convertedCoinCount);
    }
    public long TotalCoinCount { get; }
    public string Label { get; }
    public string CoinText { get; }
}

This is an adaptation from the other answer .这是对另一个答案的改编。

Then I tried to use something more absstract, you can just edit the list of suffixes, and will act accordingly, by step of thousand factor.然后我尝试使用更抽象的东西,你可以只编辑后缀列表,然后按照千因素的步骤进行相应的操作。

Test it on rextester在 rextester 上测试

    public class Program
    {
        public static void Main(string[] args)
        {
            var coinDescriptor = new CoinDescriptor();
            
            coinDescriptor.TotalCoins = 42;
            
            for (int i = 1; i < 16; i++)
            {
                coinDescriptor.TotalCoins *= 10;
                Console.WriteLine(coinDescriptor.DisplayText);
            }
            
        }
    }
    
    public class CoinDescriptor
    {
        private static readonly string[] suffixes = new string[] { "", "k", "M", "G", "T" };
        
        private long totalCoins;
        public long TotalCoins 
        { 
            get => totalCoins;
            set 
            { 
                totalCoins = value;
                UpdateDisplayText();
            }
        }
        
        public string DisplayText { get; private set; }  
        
        private void UpdateDisplayText()
        {
            int numberOfThousandExponent = (int) Math.Min(suffixes.Length - 1, Math.Floor(Math.Log10(totalCoins) / 3));
            string suffix = suffixes[numberOfThousandExponent];
            decimal valueDisplayed = (decimal) Math.Round(totalCoins / Math.Pow(1000d, numberOfThousandExponent), 2);
            DisplayText = valueDisplayed.ToString(CultureInfo.InvariantCulture) + suffix;
        }
    }

I didn't use a "Label" text field, just to show a different way to build the display text, but frankly I prefer William Edmondson's way in this matter.我没有使用“标签”文本字段,只是为了展示构建显示文本的不同方式,但坦率地说,在这件事上我更喜欢 William Edmondson 的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM