An warware: zagaye lambobi zuwa n lambobi

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 da ceil() da kuma floor(). The ceil() aiki yana zagaye lamba sama, yayin da floor() 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 kamar quantize() da kuma round() 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.

Shafi posts:

Leave a Comment