입력 숫자를 단위에 따라 변환.
K - 1,000
M - 1,000,000
B - 1,000,000,000
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NumberTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("1,000 => " + ChangeNumber("1000"));
Debug.Log("123,456 => " + ChangeNumber("123456"));
Debug.Log("1,000,000 => " + ChangeNumber("1000000"));
Debug.Log("1,000,000,000 => " + ChangeNumber("1000000000"));
}
public static string ChangeNumber(string number)
{
char[] unitAlphabet = new char[3] { 'K', 'M', 'B' };
int unit = 0;
while (number.Length > 6)
{
unit++;
number = number.Substring(0, number.Length - 3);
}
if (number.Length > 3)
{
int newInt = int.Parse(number);
if (number.Length > 4)
{
return (newInt / 1000).ToString() + unitAlphabet[unit];
}
else
{
return (newInt / 1000f).ToString("0.0") + unitAlphabet[unit];
}
}
else
{
int newInt = int.Parse(number);
return (newInt).ToString();
}
}
}
결과
'Unity' 카테고리의 다른 글
[Unity] 자주쓰는 유니티 오픈소스 에셋(Git) (0) | 2023.02.02 |
---|---|
[Unity] LineRenderer 를 활용해 화살표 그리기! (0) | 2023.02.02 |
[Unity] 반지름 R인 원 위에 일정 간격(각도)의 위치 얻기. (0) | 2023.02.02 |
[Unity]3D 오브젝트를 UI 위치로 이동시키기. (0) | 2023.02.01 |