본문 바로가기
개발/C#

유니티 C# 반올림, 올림, 내림, 소수점 2자리 버리기 간단 구현

by SPNK 2023. 2. 18.
반응형
  • 반올림 Round
float myFloat = 3.6f;
int myInt = Mathf.RoundToInt(myFloat);
Debug.Log(myInt); // 출력: 4

 

  • 반올림 Ceil
float myFloat = 3.2f;
int myInt = Mathf.CeilToInt(myFloat);
Debug.Log(myInt); // 출력: 4

 

  • 내림 Floor
float myFloat = 3.8f;
int myInt = Mathf.FloorToInt(myFloat);
Debug.Log(myInt); // 출력: 3

 

  • 소수점 전부 버리기
float myFloat = 3.14159f;
int myInt = Mathf.FloorToInt(myFloat);
Debug.Log(myInt); // 출력: 3

 

  • 소수점 1자리 버리기
float myFloat = 123.45678f;
float truncatedFloat = Mathf.Floor(myFloat * 10f) /  10f;
Debug.Log(truncatedFloat); // 출력: 123.4

 

  • 소수점 2자리 버리기
float myFloat = 123.45678f;
float truncatedFloat2 = Mathf.Floor(myFloat * 100f) /  100f;
Debug.Log(truncatedFloat2); // 출력: 123.45

또는
myFloat = float.Parse(myFloat.ToString("N1"));

 


참고할만한 글

유니티 C# 두 점 사이의 각도 구하기 간단 구현 Unity Vector2 Angle

코드 작성 using UnityEngine; public class AngleCalculator : MonoBehaviour { public Transform pointA; public Transform pointB; void Start() { Vector2 direction = pointB.position - pointA.position; float angle = Vector2.Angle(Vector2.right, direction); D

parksh3641.tistory.com

유니티 C# Enum Count 길이 간단 구하기

코드 작성 using System.Collections; using System.Collections.Generic; using UnityEngine; public enum MoneyType { Gold = 0, Crystal } public class ExampleEnum : MonoBehaviour { public MoneyType moneyType = MoneyType.Gold; void GetEnumCount() { int count

parksh3641.tistory.com

 

반응형

댓글