1. 실행 이미지(1)
2. 실행 이미지(2)

 

using UnityEditor;
using UnityEngine;
using System.Numerics;
using System.Collections.Generic;
using System.Text;
using System.Linq;

public class BigNumberConverter : EditorWindow
{
    private string inputString = "";
    private string resultString = "";
    private string numberA = ""; // 첫 번째 숫자 입력 필드
    private string numberB = ""; // 두 번째 숫자 입력 필드
    private string calculationResult = ""; // 계산 결과 필드

    private UnityEngine.Vector2 scrollPosition;

    private List<string> suffixes = new List<string>(new[] { "" });

    [MenuItem("Tools/BigNumberConverter")]
    public static void ShowWindow()
    {
        GetWindow<BigNumberConverter>("Big Number Converter");
    }

    void OnGUI()
    {
        GUILayout.Label("Ver. 1.2.0", EditorStyles.boldLabel);
        DrawLine();

        GUILayout.Label("Big Number Converter", EditorStyles.boldLabel);


        GUIStyle textAreaStyle = new GUIStyle(EditorStyles.textArea)
        {
            wordWrap = true
        };

        scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(100));
        inputString = EditorGUILayout.TextArea(inputString, textAreaStyle, GUILayout.ExpandHeight(true));
        EditorGUILayout.EndScrollView();

        if (GUILayout.Button("Convert"))
        {
            resultString = ConvertToBigNumberFormat(inputString);
        }

        EditorGUILayout.LabelField("Converted Number:", resultString);

        GUILayout.Space(10);
        DrawLine();

        GUILayout.Label("Big Number calculator", EditorStyles.boldLabel);
        // 사칙연산을 위한 추가 기능
        GUILayout.Label("Number A:");
        numberA = EditorGUILayout.TextArea(numberA, textAreaStyle);
        GUILayout.Label("Number B:");
        numberB = EditorGUILayout.TextArea(numberB, textAreaStyle);

        GUILayout.BeginHorizontal();
        if (GUILayout.Button("+")) PerformCalculation("+");
        if (GUILayout.Button("-")) PerformCalculation("-");
        if (GUILayout.Button("*")) PerformCalculation("*");
        if (GUILayout.Button("/")) PerformCalculation("/");
        GUILayout.EndHorizontal();

        EditorGUILayout.LabelField("Calculation Result:", calculationResult);
    }

    private string ConvertToBigNumberFormat(string input)
    {
        BigInteger number;
        if (BigInteger.TryParse(input, out number))
        {
            return NumberToBigNumberFormat(number);
        }
        else
        {
            return "Invalid Input";
        }
    }

    private string NumberToBigNumberFormat(BigInteger number)
    {
        if (number == 0) return "0";

        int unitIndex = 0;
        double value = (double)number;


        while (value >= 1000)
        {
            value /= 1000;
            unitIndex++;
        }


        while (unitIndex >= suffixes.Count)
        {
            ExtendSuffixes();
        }

        string suffix = unitIndex > 0 ? suffixes[unitIndex - 1] : ""; 


        string format = "0.##";
        string result = value.ToString(format) + suffix;

        return result;
    }
    private void PerformCalculation(string operation)
    {
        BigInteger a, b;
        bool isANumber = BigInteger.TryParse(numberA, out a);
        bool isBNumber = BigInteger.TryParse(numberB, out b);

        if (!isANumber || !isBNumber)
        {
            calculationResult = "Invalid Input";
            return;
        }

        switch (operation)
        {
            case "+":
                calculationResult = (a + b).ToString();
                break;
            case "-":
                calculationResult = (a - b).ToString();
                break;
            case "*":
                calculationResult = (a * b).ToString();
                break;
            case "/":
                if (b == 0)
                    calculationResult = "Cannot divide by zero";
                else
                    calculationResult = (a / b).ToString();
                break;
            default:
                calculationResult = "Unknown operation";
                break;
        }
    }


    private void ExtendSuffixes()
    {
        if (suffixes.LastOrDefault() == "")
        {
            suffixes[0] = "A";
        }
        else
        {
            string lastSuffix = suffixes.Last();
            char lastChar = lastSuffix.Last();
            if (lastChar < 'Z')
            {
                suffixes.Add(lastSuffix.Substring(0, lastSuffix.Length - 1) + (char)(lastChar + 1));
            }
            else
            {
                if (lastSuffix.All(c => c == 'Z'))
                {
                    suffixes.Add(new string('A', lastSuffix.Length + 1));
                }
                else
                {
                    int index = lastSuffix.Length - 1;
                    while (lastSuffix[index] == 'Z')
                    {
                        index--;
                    }
                    string nextSuffix = lastSuffix.Substring(0, index) + (char)(lastSuffix[index] + 1);
                    nextSuffix = nextSuffix.PadRight(lastSuffix.Length, 'A');
                    suffixes.Add(nextSuffix);
                }
            }
        }
    }
    private void DrawLine()
    {
        var rect = GUILayoutUtility.GetRect(1, 1, GUILayout.ExpandWidth(true));
        EditorGUI.DrawRect(rect, Color.gray);
    }
}

 

 

+ Recent posts