Java 17 Recipes


CHAPTER 4 Numbers and Dates


Download 3.2 Mb.
Pdf ko'rish
bet66/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   62   63   64   65   66   67   68   69   ...   245
Bog'liq
Java 17 Recipes

CHAPTER 4
Numbers and Dates
This chapter helps you understand how to perform some of the most basic operations 
with numbers and dates that play a significant role in many applications. You learn to 
work with date, time, and time zone data. Moreover, the chapter provides examples 
that work with different kinds of numbers and format them to fit most situations. It also 
provides insight on performing advanced tasks such as working with currency.
4-1. Rounding Float and Double Values to Integers
 Problem
You need to round floating-point numbers or doubles in your application to 
integer values.
 Solution
Use one of the java.lang.Math round() methods to round the number into the format 
you require. The Math class has two methods that can be used for rounding floating- 
point numbers or double values. The following code demonstrates how to use each of 
these methods.
public static int roundFloatToInt(float myFloat){
return Math.round(myFloat);
}
public static long roundDoubleToLong(double myDouble){
return Math.round(myDouble);
}


108
The first method, roundFloatToInt(), accepts a floating-point number and 
uses the java.lang.Math class to round that number to an int. The second method, 
roundDoubleToLong(), accepts a double value and uses the java.lang.Math class to 
round that double value to a long value.
The following is the main class.
public static void main(String[] args){
Float floatValue = 7.82f;
Double doubleValue = 9.9d;
System.out.println(roundFloatToInt(floatValue));
System.out.println(roundDoubleToLong(doubleValue));
}
The result is:
8
10
 How It Works
The java.lang.Math class contains plenty of helper methods to make our lives easier 
when working with numbers. The round() methods are no exception since they can 
easily round floating-point or double values. One version of the java.lang.Math 
round() method accepts a float as an argument. It rounds the float to the closest int 
value, with ties rounding up. If the argument is not a number (NaN), a zero is returned. 
When arguments that are positive or negative infinity are passed into a round() method, 
a result equal to the value of Integer.MAX_VALUE or Integer.MIN_VALUE, respectively, is 
returned. The second version of the java.lang.Math round() method accepts a double 
value, which is rounded to the closest long value, with ties rounding up. Like the other 
round() method, if the argument is NaN, a zero is returned. Similarly, when positive 
or negative infinity arguments are passed into a round() method, a result is equal to a 
Long.MAX_VALUE or Long.MIN_VALUE value is returned.
Chapter 4 Numbers aNd dates


109
Note NaN, pOsItIVe_INFINItY, and NeGatIVe_INFINItY are constant values 
defined within the Float and Double classes. NaN (Not a Number) is an 
undefined or unrepresentable value. For example, a NaN value can be produced 
by dividing 0.0f by 0.0f. the values represented by pOsItIVe_INFINItY and 
NeGatIVe_INFINItY refer to values produced by operations that generate such 
extremely large or negative values of a particular type (floating-point or double) 
that they cannot be represented normally: the output is Infinity or –Infinity. For 
instance, 1.0/0.0 or –1.0/0.0 would produce such values. In fact, Java uses some 
special numeric values to handle the floating-point arithmetic results of such an 
operation due to its specification of the division operations. On the other hand, an 
arithmeticexception error is thrown for the integer. test the following code.
public static void main(String[] args){
Float floatValue1 = 1.0f;
Float floatValue1n = -1.0f;
Float floatValue0 = 0.0f;
System.out.println(floatValue1/floatValue0);
System.out.println(floatValue0/floatValue0);
System.out.println(floatValue1n/floatValue0);
Double doubleValue1 = 1.0d;
Double doubleValue1n = -1.0d;
Double doubleValue0 = 0.0d;
System.out.println(doubleValue1/doubleValue0);
System.out.println(doubleValue0/doubleValue0);
System.out.println(doubleValue1n/doubleValue0);
}
The following is the output.
Infinity
NaN
-Infinity
Infinity
NaN
-Infinity
Chapter 4 Numbers aNd dates


110

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   62   63   64   65   66   67   68   69   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling