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


Scala Int round()用法及代碼示例

round()方法用於返回指定int值的舍入值。使用此方法可避免因通過浮點數而繞道而導致的精度意外損失。

函數定義: def round:Int

返回類型: It returns the rounded value of the specified int value.



範例1:

// Scala program of Int round 
// method  
    
// Creating object  
object GfG  
{   
    
    // Main method  
    def main(args:Array[String])  
    {  
        
        // Applying round method  
        val result = (5).round 
            
            
        // Displays output  
        println(result)  
        
    }  
}  
輸出:
5

範例2:

// Scala program of Int round 
// method  
    
// Creating object  
object GfG  
{   
    
    // Main method  
    def main(args:Array[String])  
    {  
        
        // Applying round method  
        val result = (5.8).round 
            
            
        // Displays output  
        println(result)  
        
    }  
} 
輸出:
6


相關用法


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