Java matemātikas kārta ()

Java Math round () metode noapaļo norādīto vērtību līdz tuvākajai int vai long vērtībai un atgriež to.

Tas ir, 1,2 ir noapaļots līdz 1 un 1,8 ir noapaļots līdz 2 .

Metodes sintakse round()ir šāda:

 Math.round(value)

Šeit round()ir statiska metode. Tādējādi mēs piekļūstam metodei, izmantojot klases nosaukumu Math,.

apaļa () parametri

round()Metode ņem vienu parametru.

  • vērtība - skaitlis, kas jānoapaļo

Piezīme : datu veids vērtību vajadzētu būt vai nu float, vai double.

kārta () Atgriešanās vērtība

  • atgriež intvērtību, ja arguments irfloat
  • atgriež longvērtību, ja arguments irdouble

round()Metode:

  • noapaļo uz augšu, ja vērtība aiz komata ir lielāka vai vienāda ar 5
     1.5 => 2 1.7 => 2 
  • noapaļo uz leju, ja vērtība aiz komata ir mazāka par 5
     1.3 => 1

1. piemērs: Java Math.round () ar dubulto

 class Main ( public static void main(String() args) ( // Math.round() method // value greater than 5 after decimal double a = 1.878; System.out.println(Math.round(a)); // 2 // value equals to 5 after decimal double b = 1.5; System.out.println(Math.round(b)); // 2 // value less than 5 after decimal double c = 1.34; System.out.println(Math.round(c)); // 1 ) )

2. piemērs: Java Math.round () ar pludiņu

 class Main ( public static void main(String() args) ( // Math.round() method // value greater than 5 after decimal float a = 3.78f; System.out.println(Math.round(a)); // 4 // value equals to 5 after decimal float b = 3.5f; System.out.println(Math.round(b)); // 4 // value less than 5 after decimal float c = 3.44f; System.out.println(Math.round(c)); // 3 ) )

Ieteicamās konsultācijas

  • Math.floor ()
  • Math.ceil ()

Interesanti raksti...