📌  相关文章
📜  Java中的 ChoiceFormat previousDouble() 方法及示例

📅  最后修改于: 2022-05-13 01:54:49.904000             🧑  作者: Mango

Java中的 ChoiceFormat previousDouble() 方法及示例

Java.text.ChoiceFormat类的previousDouble()方法用于获取刚好小于传递的双精度值的双精度值。
句法:

public static final double previousDouble(double d)

参数:此方法接受双精度值d作为返回前一个双精度值的参数。
返回值:此方法返回一个比传递的双精度略小的双精度值。
以下是说明previousDouble()方法的示例:
示例 1:

Java
// Java program to demonstrate
// previousDouble() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        // getting double value just
        // lesser than the passed value
        // using previousDouble() method
        double value
            = ChoiceFormat.previousDouble(22);
 
        // display the result
        System.out.print("Lesser double value: "
                         + value);
    }
}


Java
// Java program to demonstrate
// previousDouble() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        // calling getValue() method
        getValue(1.23d);
        getValue(10d);
        getValue(-12d);
        getValue(1.2f);
        getValue(50);
    }
 
    // defining getValue() method
    public static void getValue(double doub)
    {
 
        // getting double value just
        // lesser than the passed value
        // using previousDouble() method
        double value
            = ChoiceFormat.previousDouble(doub);
 
        // display the result
        System.out.println("Just lesser than "
                           + doub + ": "
                           + value);
    }
}


输出:
Lesser double value: 21.999999999999996

示例 2:

Java

// Java program to demonstrate
// previousDouble() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        // calling getValue() method
        getValue(1.23d);
        getValue(10d);
        getValue(-12d);
        getValue(1.2f);
        getValue(50);
    }
 
    // defining getValue() method
    public static void getValue(double doub)
    {
 
        // getting double value just
        // lesser than the passed value
        // using previousDouble() method
        double value
            = ChoiceFormat.previousDouble(doub);
 
        // display the result
        System.out.println("Just lesser than "
                           + doub + ": "
                           + value);
    }
}
输出:
Just lesser than 1.23: 1.2299999999999998
Just lesser than 10.0: 9.999999999999998
Just lesser than -12.0: -12.000000000000002
Just lesser than 1.2000000476837158: 1.2000000476837156
Just lesser than 50.0: 49.99999999999999

参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/ChoiceFormat.html#previousDouble-double-