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


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


setParseIntegerOnly()方法是java.text.NumberFormat的內置方法,該方法設置是否僅將數字解析為整數。

用法:

public void setParseIntegerOnly(boolean val)

參數:該函數接受強製性參數val,該參數指定要設置的值


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

下麵是上述函數的實現:

程序1

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

示例2:

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

參考: https://docs.oracle.com/javase/10/docs/api/java/text/NumberFormat.html#setParseIntegerOnly(boolean)



相關用法


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