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


underscore.js _.isFloat()用法及代碼示例

_。isFloat()方法檢查給定值是否為Float值,然後返回對應的布爾值。

用法:

_.isFloat( value );

參數:

  • value:給定值以檢查浮點值。

返回值:此方法返回一個布爾值(如果給定值是偶數,則返回true,否則返回false)。

注意:由於它需要安裝underscore.js contrib庫,因此在正常的JavaScript中將無法使用。 Underscore.js contrib庫可以使用安裝npm安裝underscore-contrib。



範例1:

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
// Checking 
console.log("The Value is Float:" 
    + _.isFloat(4.91));

輸出:

The Value is Float:true

範例2:

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
// Checking 
console.log("The Value is Float:" 
    + _.isFloat(21));

輸出:

The Value is Float:false

範例3:對於此方法,值2.0和2都將被視為整數,因此此方法將返回false。

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
// Checking 
console.log("The Value is Float:" 
    + _.isFloat(2.0));

輸出:

The Value is Float:false

範例4:包含float值的字符串將返回true。

Javascript

// Defining underscore contrib variable 
var _ = require('underscore-contrib');  
  
// Checking 
console.log("The Value is Float:" 
    + _.isFloat("2.188"));

輸出:

The Value is Float:true

相關用法


注:本文由純淨天空篩選整理自taran910大神的英文原創作品 Underscore.js _.isFloat() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。