สมัครเรียนโทร. 085-350-7540 , 084-88-00-255 , ntprintf@gmail.com


Rounding Decimals

เรียนเขียนโปรเเกรมง่ายๆกับ Expert Progamming Tutor กับบท Rounding Decimals
มี 5 วิธีหลักในการปัดเศษทศนิยมใน NumPy
- truncation
- fix
- rounding
- floor
- ceil
truncation
ตัดให้เป็นจำนวนเต็มใกล้กับศูนย์มากที่สุด ใช้ฟังก์ชั่น trunc() และ fix()
ตัวอย่าง
ตัดองค์ประกอบของอาร์เรย์ต่อไปนี้

import numpy as np
arr = np.trunc([-3.1666, 3.6667])
print(arr)

ผลลัพธ์

[-3.  3.]

Rounding
การเพิ่มฟังก์ชัน around() ก่อนหน้าหลักหรือทศนิยม 1 ถ้า> = 5 ไม่ทำอะไรเลย เช่น ปัดเศษเป็นทศนิยม 1 ตำแหน่ง 3.16666 คือ 3.2
ตัวอย่าง
ปัดเศษทศนิยม 3.1666 ถึง 2 ตำแหน่ง

import numpy as np
arr = np.around(3.1666, 2)
print(arr)

ผลลัพธ์

3.17

Floor
ฟังก์ชั่น floor () ปัดเศษทศนิยมเป็นจำนวนเต็มล่างที่ใกล้ที่สุด เช่น ชั้น 3.166 คือ 3
ตัวอย่าง
ชั้นองค์ประกอบของอาร์เรย์ต่อไปนี้

import numpy as np
arr = np.floor([-3.1666, 3.6667])
print(arr)

ผลลัพธ์

[-4.  3.]

หมายเหตุ: ฟังก์ชั่น floor() จะส่งกลับค่าลอยตัวซึ่งแตกต่างจากฟังก์ชัน trunc() ที่ส่งคืนจำนวนเต็ม
Ceil
ฟังก์ชัน ceil() ปัดเศษทศนิยมให้เป็นจำนวนเต็มที่ใกล้เคียงที่สุด เช่น. ceil ของ 3.166 คือ 4
ตัวอย่าง
Ceil องค์ประกอบของอาร์เรย์ต่อไปนี้

import numpy as np
arr = np.ceil([-3.1666, 3.6667])
print(arr)

ผลลัพธ์

[-3.  4.]

ในเนื้อหาบทนี้จะเป็นเรื่องเกี่ยวกับการปัดเศษทศนิยม ตัดให้เป็นจำนวนเต็มใกล้กับศูนย์มากที่สุด ใช้ฟังก์ชั่น trunc() และ fix()
แปลจากhttps://www.w3schools.com/python/numpy_ufunc_rounding_decimals.asp


Tag ที่น่าสนใจ: java c# vb.net python c c++ machine_learning web database oop cloud aws ios android


บทความนี้อาจจะมีที่ผิด กรุณาตรวจสอบก่อนใช้

หากมีข้อผิดพลาด/ต้องการพูดคุยเพิ่มเติมเกี่ยวกับบทความนี้ กรุณาแจ้งที่ http://m.me/Expert.Programming.Tutor

ไม่อยากอ่าน Tutorial อยากมาเรียนเลยทำอย่างไร?

สมัครเรียน ONLINE ได้ทันทีที่ https://elearn.expert-programming-tutor.com

หรือติดต่อ

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM

แผนที่ ที่ตั้งของอาคารของเรา

แผนผังการเรียนเขียนโปรแกรม

Link อื่นๆ

Allow sites to save and read cookie data.
Cookies are small pieces of data created by sites you visit. They make your online experience easier by saving browsing information. We use cookies to improve your experience on our website. By browsing this website, you agree to our use of cookies.

Copyright (c) 2013 expert-programming-tutor.com. All rights reserved. | 085-350-7540 | 084-88-00-255 | ntprintf@gmail.com

ติดต่อเราได้ที่

085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM
แผนที่ ที่ตั้งของอาคารของเรา