Math
Server & Client Supported
ภาพรวม
ฟังก์ชันสำหรับการคำนวณและจัดรูปแบบตัวเลขต่างๆ
ที่อยู่ไฟล์
source/modules/common/math.lua
ฟังก์ชัน
PositiveOnly
เอาเฉพาะค่าบวก ถ้าเป็นค่าลบจะคืนค่า 0
value
number
ค่าที่ต้องการตรวจสอบ
ค่าที่ส่งกลับ: number
- ค่าบวกหรือ 0
-- ตัวอย่างการใช้งาน
print(AFUCore.Math.PositiveOnly(5)) -- 5
print(AFUCore.Math.PositiveOnly(-3)) -- 0
print(AFUCore.Math.PositiveOnly(0)) -- 0
Round
ปัดเศษตัวเลขตามจำนวนทศนิยมที่กำหนด
value
number
ตัวเลขที่ต้องการปัดเศษ
numDecimalPlaces
number
จำนวนตำแหน่งทศนิยม
ค่าที่ส่งกลับ: number
- ตัวเลขที่ถูกปัดเศษแล้ว
-- ตัวอย่างการใช้งาน
print(AFUCore.Math.Round(3.14159, 2)) -- 3.14
print(AFUCore.Math.Round(3.14159, 0)) -- 3
print(AFUCore.Math.Round(3.14159)) -- 3
GroupDigits
แบ่งหลักตัวเลขด้วยเครื่องหมาย , (หรือตามที่กำหนดใน locale)
value
number
ตัวเลขที่ต้องการแบ่งหลัก
ค่าที่ส่งกลับ: string
- ตัวเลขที่ถูกแบ่งหลักแล้ว
-- ตัวอย่างการใช้งาน
print(AFUCore.Math.GroupDigits(1000)) -- "1,000"
print(AFUCore.Math.GroupDigits(1000000)) -- "1,000,000"
print(AFUCore.Math.GroupDigits(1234.56)) -- "1,234.56"
Trim
ตัดช่องว่างหน้า-หลังข้อความ หรือตัดช่องว่างทั้งหมด
value
string
ข้อความที่ต้องการตัดช่องว่าง
allSpaces
boolean
true = ตัดทุกช่องว่าง, false = ตัดเฉพาะหน้า-หลัง
ค่าที่ส่งกลับ: string
- ข้อความที่ถูกตัดช่องว่างแล้ว
-- ตัวอย่างการใช้งาน
print(AFUCore.Math.Trim(" hello ")) -- "hello"
print(AFUCore.Math.Trim(" hello world ")) -- "hello world"
print(AFUCore.Math.Trim(" hello world ", true)) -- "helloworld"
FormatMs
แปลงเวลาจากมิลลิวินาทีเป็นรูปแบบ HH:MM:SS
ms
number
เวลาในหน่วยมิลลิวินาที
ค่าที่ส่งกลับ: string
- เวลาในรูปแบบ HH:MM:SS
-- ตัวอย่างการใช้งาน
print(AFUCore.Math.FormatMs(3600000)) -- "01:00:00"
print(AFUCore.Math.FormatMs(65000)) -- "00:01:05"
print(AFUCore.Math.FormatMs(5000)) -- "00:00:05"
FormatMsTag
แปลงเวลาจากมิลลิวินาทีเป็นข้อความ (hr, hrs, min, mins, sec, secs)
ms
number
เวลาในหน่วยมิลลิวินาที
ค่าที่ส่งกลับ: string
- เวลาในรูปแบบข้อความ
-- ตัวอย่างการใช้งาน
print(AFUCore.Math.FormatMsTag(3600000)) -- "1 hr"
print(AFUCore.Math.FormatMsTag(7200000)) -- "2 hrs"
print(AFUCore.Math.FormatMsTag(60000)) -- "1 min"
print(AFUCore.Math.FormatMsTag(120000)) -- "2 mins"
print(AFUCore.Math.FormatMsTag(1000)) -- "1 sec"
print(AFUCore.Math.FormatMsTag(2000)) -- "2 secs"
หมายเหตุ
ฟังก์ชันเหล่านี้ช่วยในการจัดการตัวเลขและการแสดงผล
GroupDigits ใช้เครื่องหมายตามการตั้งค่าภาษา (locale)
FormatMs และ FormatMsTag ใช้สำหรับแสดงเวลาในรูปแบบที่อ่านง่าย