Float class trong java

Float class là một wrapper class của kiểu dữ liệu nguyên thủy float chứa nhiều method giúp chúng ta thao tác dễ dàng hơn float value. Hơn nữa chúng ta chỉ có thể sử dụng các ArrayList, LinkedList etc với float value thông qua Float class vì chúng ta thao tác với object. Mỗi object Float chứa duy nhất một float value.

Constructor

Để khởi tạo object Float class, chúng ta có 2 cách chính như sau

1, Float(float b):  Khởi tạo Float object từ một float value được cung cấp.

// Syntax
public Float(int b);
 
// Ví dụ
Long l = new Float(10.2324248); // 10.232425

2, Float(String s): Khởi tạo Float object từ chuỗi float value. Nếu chuỗi không hợp lệ sẽ gây ra lỗi NumberFormatException.

public Float(String s) throws NumberFormatException

Float f = new Float("10.2324248"); //10.232425

Giá trị float lớn nhất – nhỏ nhất

Float class còn cung cấp cho chúng ta 2 hằng số mà được sử dụng nhiều khi thao tác với kiểu dữ liệu float là giá trị lớn nhất và bé nhất.

Giá trị lớn nhất: Float.MAX_VALUE (3.4028235E38).

Giá trị nhỏ nhất: Float.MIN_VALUE (1.4E-45).

Các method trong Float class

Float class thừa kế từ Number class nên ngoài các method intValue()longValue()doubleValue() etc thì Float class còn cung cấp cho chúng ta rất nhiều method. Sau đây mình sẽ liệt kê một số method mà mình thường sử dụng trong quá trình code java:

1, toString(): Trả về String số tương ứng với giá trị float.

Syntax

public String toString()

Example

Float number = new Float (10.213);
System.out.println(number.toString()); // 10.213

2, valueOf(): Khởi tạo một object Float từ giá trị float cung cấp. Nếu giá trị cung cấp không hợp lệ thì sẽ gây ra lỗi NumberFormatException.

Syntax

public static Float valueOf(float i)
 
// or 
 
public static Float valueOf(String s) throws NumberFormatException

Example

System.out.println(Float.valueOf("100.214")); // 100

System.out.println(Float.valueOf(100.214f)); // 100

System.out.println(Float.valueOf("aaa")); // NumberFormatException

3, parseFloat(): Trả về float value từ một chuỗi số được cung cấp. Nếu giá trị được cung cấp không hợp lệ gây ra lỗi NumberFormatException.

Syntax

public static float parseFloat(String s) throws NumberFormatException

Example

System.out.println(Float.parseFloat("100")); // 100

System.out.println(Float.parseFloat("aaa")); // NumberFormatException

4, isNaN(): Trả về true nếu float object không phải là số, ngược lại false.

Syntax

public boolean isNaN()

Example

Float f1 = new Float(-1.0/0.0);
Float f2 = new Float(1.0/0.0);
Float f3 = new Float(0.0/0.0);


System.out.println(f1 + " = " + f1.isNaN()); // -Infinity = false

System.out.println(f2 + " = " + f2.isNaN()); // Infinity = false

System.out.println(f3 + " = " + f3.isNaN()); // NaN = true

5, isInfinite(): Trả về true nếu float object chứa giá trị rất lớn(+-), ngược lại false.

Syntax

public boolean isInfinite()

Example

Float f1 = new Float(-1.0/0.0);
Float f2 = new Float(1.0/0.0);
Float f3 = new Float(0.0/0.0);


System.out.println(f1 + " = " + f1.isInfinite()); // -Infinity = true

System.out.println(f2 + " = " + f2.isInfinite()); // Infinity = true

System.out.println(f3 + " = " + f3.isInfinite()); // NaN = false

6, equals(): So sánh bằng giữa 2 object Float.

Syntax

public boolean equals(Object obj)

Example

Float a = Float.valueOf(100.232f);

Float b = Float.valueOf(100.2f);

Float c = Float.valueOf(100.232f);

System.out.println(a.equals(b)); // false

System.out.println(a.equals(c)); // true

7, compareTo(): So sánh 2 object Float. Trả về -1 nếu nhỏ hơn, 1 lớn hơn, 0 bằng.

Syntax

public int compareTo(Float anotherFloat)
Float a = Float.valueOf(100.232f);

Float b = Float.valueOf(100.2f);

Float c = Float.valueOf(100.232f);

System.out.println(a.compareTo(b)); // 1

System.out.println(b.compareTo(a)); // -1

System.out.println(a.compareTo(c)); // 0

8, max(): Lấy số lớn nhất trong 2 số, nếu 2 số trùng nhau thì trả về một trong hai.

Syntax

public static float max(float a, float b)

Example

System.out.println(Float.max(100.43f, 90.43f)); // 100.43

System.out.println(Float.max(100.43f, 100.43f)); // 100.43

9, min(): Lấy số nhỏ nhất trong 2 số, nếu 2 số trùng nhau thì trả về một trong hai.

Syntax

public static float min(float a, float b)
System.out.println(Float.min(100.43f, 90.43f)); // 90.43

System.out.println(Float.min(100.43f, 100.43f)); // 100.43

10, sum(): Trả về tổng của 2 số float.

Syntax

public static float sum(float a, float b)

Example

System.out.println(Float.sum(100.43f, 90.43f)); // 190.86

Abstract method

Ngoài các method trên thì Float class cũng implement các method của Number abstract class. Các method này sẽ giúp chúng ta chuyển giá trị kiểu long sang các kiểu dữ liệu khác như double, float, long để thao tác:

  1. public byte byteValue(): Trả về byte tương ứng của Integer object.
  2. public short shortValue(): Trả về short value tương ứng của Integer object.
  3. public int intValue(): Trả về int value tương ứng của Integer object.
  4. public long longValue(): Trả về long value tương ứng của Integer object.
  5. public double doubleValue(): Trả về double value tương ứng của Integer object.
  6. public float shortValue(): Trả về float value tương ứng của Integer object.

Ví dụ

public class Main {

    public static void main(String[] args) {

        Float l = Float.valueOf(1000.43f);
        System.out.println("Byte value: " + l.byteValue());
        System.out.println("Short value: " + l.shortValue());
        System.out.println("Int value: " + l.intValue());
        System.out.println("Long value: " + l.longValue());
        System.out.println("Double value " + l.doubleValue());
        System.out.println("Float value: " + l.floatValue());

        double a = 5.5d;
        double d = l.doubleValue()/a;
        System.out.println(d);

        float b = 5.5f;
        float f = l.floatValue()/b;
        System.out.println(f);
    }
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x