Java harshe ne mai ฦarfi kuma mai sassauฦa, mai daidaitawa ga buฦatu daban-daban kuma ana amfani da shi sosai a cikin duniyar ci gaba. Daga cikin ayyukansa masu yawa, wanda ya fi dacewa shine ikon canza nau'ikan bayanai. Misali na yau da kullun shine jujjuya nau'in bayanai na 'biyu' zuwa nau'in bayanai mai tsayi. Wannan juzu'i na iya amfani da dalilai da yawa; babba, yana ba da damar haษaka amincin bayanan bayanai, rage damar kurakurai na lissafi, waษanda suke da alama marasa ฦarfi amma suna iya yin tasiri sosai ga sakamakon aiki. Bayan haka, waษannan jujjuyawar kuma na iya ba da hanya don dacewa tsakanin tsarin ko kayayyaki daban-daban.
Tambayar farko da ta taso ita ce - Ta yaya mutum zai yi wannan jujjuya daga 'biyu' zuwa 'dogon'?
Hanyar Java โ Sau biyu zuwa Dogon Juya
Java yana ba da hanyar da aka gina don aiwatar da wannan juyawa; hanyar ita ce madaidaiciya kuma mai sauฦin amfani. Wannan hanyar wani bangare ne na ajin 'java.lang.Double' kuma yana biyan bukatun wannan jujjuya kai tsaye.
double testDouble = 152.65; long convertedLong = (long) testDouble; System.out.println(convertedLong);
A cikin misalin da ke sama, muna da ฦima biyu da aka adana a ma'aunin 'testDouble'. Ta hanyar sanya '(dogon)' kawai kafin 'testDouble', Java ta fahimci shi azaman umarni don canza 'testDouble' zuwa nau'in bayanai mai tsawo. Ana adana sakamakon wannan aiki a cikin 'convertedLong', wanda daga baya ya sami bugawa.
Bayanin mataki-mataki na Code
Lambar don wannan jujjuya tana da sauฦi mai sauฦi kuma ana iya fahimta cikin sauฦi idan aka rushe zuwa sassa.
1. Sanarwa mai sauyi biyu: Mun fara ayyana mabambanta biyu 'testDouble' kuma mu sanya shi darajar 152.67.
double testDouble = 152.65;
2. Juyawa zuwa Doguwa: Ana samun wannan ta hanyar tantance nau'in bayanan da muke son musanya zuwa, a cikin baka, kafin mabambantan da ke adana darajar. A cikin yanayinmu, wannan nau'in bayanan yana da tsawo.
long convertedLong = (long) testDouble;
3. fitarwa: A ฦarshe, muna amfani da umarnin System.out.println() don buga ฦimar da aka canza.
System.out.println(convertedLong);
Sauran Laburaren Ko Ayyukan da Suka Shafi
Java yana ba da wasu hanyoyi da hanyoyi da yawa waษanda ke taimakawa wajen canza nau'in bayanai, wasu daga cikinsu sune:
- Double.longValue(): Wannan hanyar tana mayar da ฦimar lambobi da wannan abu ke wakilta bayan juyowa zuwa nau'i mai tsayi.
- Math.round(): Hakanan za'a iya amfani da wannan hanyar don jujjuyawa, amma tana zagaye lambar zuwa mafi kusa tsawon tsayi.
Ka tuna, yayin da ake jujjuya daga 'biyu' zuwa 'dogon', ana yanke ฦididdigan ฦima, kuma ba a ฦarewa ba. Daidaitawar kulawa da fahimtar nau'in bayanan da aka canza shine a fasaha mai mahimmanci a cikin shirye-shiryen Java, kuma, ta tsawo, a cikin ci gaba. Yana tabbatar da aikace-aikacenku yana gudana ba tare da wata matsala ba, kuma ฦimar da aka ฦirฦira sun kasance kamar yadda ake tsammani, yana taimaka muku cimma sakamakon da ake so.