The use of C# Math and Mathf (decimal rounding, rounding, absolute value, etc.)

When we do some mathematical calculations in C#, we often see the use of Math and Mathf. Which one to use exactly, and what's the difference between them?

First understand their definitions:

Math: It is a tool class encapsulated in C# for mathematical calculation, and the namespace is System;

Mathf: It is a tool structure packaged in Unity for mathematical calculation, and the namespace is UnityEngine.

In fact, Mathf is an organization packaged by Unity based on Math. It includes all the methods in Math and adds some methods suitable for Unity's own game development.

Mainly understand some common methods of Math:

1. Decimal rounding includes rounding up and rounding down:

1. Upward rounding method: Math.Ceiling()

Example: Math.Ceiling(1)=1

Math.Ceiling(1.3)=2

Math.Ceiling(1.5)=2

2. Rounding down method: Math.Floor()

Example: Math.Floor(1)=1

Math.Floor(1.3)=1

Math.Floor(1.5)=1

Second, rounding

The method used for rounding is: Math.Round()

Guess you like

Origin blog.csdn.net/mr_five55/article/details/129250704