Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java round() Method

Java round() Method

Java Number ClassThe Java Number class


The round() method returns the closest int or long value, rounding half up.

round represents "Rounding", and the algorithm isMath.floor(x+0.5) , that is, add 0.5 to the original number and then round down, so Math.round(11.5) gives 12, and Math.round(-11.5) gives -11.

Syntax

This method has the following syntax formats:



long round(double d)



int round(float f)

Parameters

Return Value

Returns a value of the closest int or long type; the method specifies the returned data type.

Example

Example

public

class

Test

{

public

static

void

main

(

String

args

[

]

)

{

double

d

=

100

.675

;

double

e

=

100

.500

;

float

f

=

100

;

float

g

=

90

f

;

System

.

out

.

println

(

Math

.

round

(

d

)

)

;

System

.

out

.

println

(

Math

.

round

(

e

)

)

;

System

.

out

.

println

(

Math

.

round

(

f

)

)

;

System

.

out

.

println

(

Math

.

round

(

g

)

)

;

}

}

Compile the above program; the output is:



101

101

100

90


Java Number ClassThe Java Number class