An warware: sau biyu zuwa tsayi

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.

Shafi posts:

Leave a Comment