當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java NumberFormat setRoundingMode()用法及代碼示例


setParseIntegerOnly()方法是java.text.NumberFormat的內置方法,該方法設置此NumberFormat中使用的RoundingMode。處理不同舍入模式的子類應重寫此方法。

用法:

public void setRoundingMode(RoundingMode mode)

參數:該函數接受強製參數模式,該模式指定要設置的模式


返回值:該函數不返回任何內容,因此返回類型為void。

異常:該函數引發兩個異常,如下所述:

  • UnsupportedOperationException:如果默認實現始終拋出此異常
  • NullPointerException :如果roundingMode為null

下麵是上述函數的實現:

程序1

// Java program to implement 
// the above function 
  
import java.text.NumberFormat; 
import java.util.Locale; 
import java.util.Currency; 
import java.math.RoundingMode; 
  
public class Main { 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        NumberFormat nF 
            = NumberFormat.getNumberInstance(); 
  
        System.out.println("set initially as: "
                           + nF.getRoundingMode()); 
  
        // Set grouping 
        nF.setRoundingMode(RoundingMode.FLOOR); 
  
        // Print the final 
        System.out.println("set finally as: "
                           + nF.getRoundingMode()); 
    } 
}
輸出:
set initially as: HALF_EVEN
set finally as: FLOOR

示例2:

// Java program to implement 
// the above function 
  
import java.text.NumberFormat; 
import java.util.Locale; 
import java.util.Currency; 
import java.math.RoundingMode; 
  
public class Main { 
    public static void main(String[] args) 
        throws Exception 
    { 
  
        NumberFormat nF 
            = NumberFormat.getNumberInstance(); 
  
        System.out.println("set initially as: "
                           + nF.getRoundingMode()); 
  
        // Set grouping 
        nF.setRoundingMode(RoundingMode.DOWN); 
  
        // Print the final 
        System.out.println("set finally as: "
                           + nF.getRoundingMode()); 
    } 
}
輸出:
set initially as: HALF_EVEN
set finally as: DOWN

示例3:

// Java program to implement 
// the above function 
  
import java.text.NumberFormat; 
import java.util.Locale; 
import java.util.Currency; 
import java.math.RoundingMode; 
  
public class Main { 
    public static void main(String[] args) 
        throws Exception 
    { 
        try { 
  
            NumberFormat nF 
                = NumberFormat.getNumberInstance(); 
  
            System.out.println("set initially as: "
                               + nF.getRoundingMode()); 
  
            // Set grouping 
            nF.setRoundingMode(null); 
  
            // Print the final 
            System.out.println("set finally as: "
                               + nF.getRoundingMode()); 
        } 
        catch (Exception e) { 
            System.out.println("Exception is: "
                               + e); 
        } 
    } 
}
輸出:
set initially as: HALF_EVEN
Exception is: java.lang.NullPointerException

參考: https://docs.oracle.com/javase/10/docs/api/java/math/RoundingMode.html



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 NumberFormat setRoundingMode() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。