Lambobin kewayawa aiki ne na gama gari a cikin ฦididdiga na lissafi daban-daban, nazarin bayanai, da gabatar da sakamako don sa su zama abin karantawa da sauฦin fahimta. A Python, akwai hanyoyi daban-daban don zagaye lambobi zuwa takamaiman adadin lambobi. A cikin wannan labarin, za mu tattauna daya irin wannan bayani ta yin amfani da aikin zagaye kuma mu nutse zurfi cikin tsari, fahimtar lambar mataki-mataki. Bugu da ฦari, za mu bincika wasu ayyuka da ษakunan karatu waษanda ke taka muhimmiyar rawa wajen magance irin waษannan matsalolin.
Gabatarwa
Zagayawa lamba yana nufin kusantarta zuwa ฦimarta mafi kusa, yawanci a takamaiman wuri na ฦima. Misali, zagaye lambar 3.14159 zuwa wurare 2 na decimal yana ba mu 3.14. A cikin Python, aikin da aka gina round()
ana amfani da shi don zagaye lambobi zuwa takamaiman adadin lambobi.
Maganin matsalar
Don zagaye lamba zuwa n wurare na goma, zaka iya amfani da round()
aiki a cikin Python ta hanyar:
number_to_round = 3.14159 rounded_number = round(number_to_round, 2) print(rounded_number) # Output: 3.14
A cikin wannan misalin, mun ฦaddamar da lamba 3.14159 zuwa wurare 2 na decimal, wanda ya haifar da 3.14.
Bayanin mataki-mataki na lambar
1. ฦayyade lambar da kuke son zagaye (lamba_zuwa_zagaye).
2. Yi amfani da aikin zagaye () don zagaye lambar zuwa wuraren da ake so na decimal (n) ta hanyar wuce lamba da adadin wuraren goma a matsayin muhawara.
3. Ajiye sakamakon a matsayin mai canzawa (rounded_number).
4. Buga sakamakon don tabbatar da fitarwa.
Yanzu, bari mu zurfafa zurfafa cikin wasu ayyuka da ษakunan karatu masu alaฦa da wannan matsalar.
Aikin zagaye()
The round()
Aiki ginannen aikin Python ne wanda ke mayar da lamba mai yin iyo wanda aka zagaye zuwa ฦayyadadden adadin wuraren ฦima. Aikin yana ษaukar gardama biyu - lambar da za a tada shi da adadin wuraren ฦima don zagaye zuwa.
Hujja ta farko ita ce lambar da ake buฦatar tadawa, na biyun kuma na zaษi ne, yana nuna adadin wuraren da za a ษagawa. Idan ba a bayar da hujja ta biyu ba, aikin yana zagaye lamba zuwa lamba mafi kusa.
The round()
aiki ya shafi zagaye rabi zuwa madaidaici zagaye, wanda kuma aka sani da zagaye na banki, wanda ke zagaye zuwa mafi kusa koda lamba lokacin da lambar da za a zagaya ta kasance tsaka-tsaki tsakani biyu masu yuwuwar dabi'u.
Sauran Laburare da Ayyuka don Zagayawa Lambobi a Python
- ษakin karatu na lissafi: The
math
ษakin karatu a Python yana ba da ayyuka da ayyuka da yawa na lissafin lissafi. Wasu ayyukan da ke da alaฦa a cikin wannan ษakin karatu sun haษa daceil()
da kumafloor()
. Theceil()
aiki yana zagaye lamba sama, yayin dafloor()
aiki yana zagaye shi. - Laburare goma: The
Decimal
ษakin karatu yana ba da wata hanyar zagayawa, yana guje wa wasu batutuwa tare da maฦasudin wakilci mai iyo. Yana ba da madaidaicin iko akan tsarin zagaye ta amfani da ayyuka kamarquantize()
da kumaround()
kuma yana da amfani musamman a lissafin kuษi ko lokacin da kuke buฦatar cikakken iko akan zagaye.
A taฦaice, za a iya yin zagaye na lambobi a Python ta amfani da ginanniyar ciki round()
aiki. Bugu da ฦari, sauran ayyuka da ษakunan karatu kamar math
da kuma Decimal
zai iya ba da madadin hanyoyin ko mafi girma iko akan tsarin zagaye, dangane da takamaiman mahallin da buฦatun matsalar ku.