【问题标题】:How can i represent a float in JavaScript? [duplicate]如何在 JavaScript 中表示浮点数? [复制]
【发布时间】:2021-10-15 20:34:45
【问题描述】:

我是 JavaScript 的新手。

在 python 中,我可以创建一个条件来检查数字是否是这样的浮点数:

num = 1.5

if type(num) == float:
    print('is a float')

我如何在 JavaScript 中做到这一点?有可能吗?

【问题讨论】:

  • @bluejambo 是的,谢谢!

标签: javascript if-statement floating-point conditional-statements typeof


【解决方案1】:
    you can try:
    
    let x=1.5;
if(!Number.isInteger(x)) {
  console.log(`The number ${x} is a float`)
};
    
    or
    
function checkFloat(x) {
  //Check if the value is a number
   if(typeof x == 'number' &&  !isNaN(x)) {
      //Check if is integer
     if(Number.isInteger(x)) {
     //print the integer
        console.log(`${x} is integer`);
      }else {
        //print the float number
        console.log(`${x} is a float`);
      };
   }else {
     //print the value that is not a number
      console.log(`${x} is not a number`);
    };
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 2021-10-10
    • 1970-01-01
    • 2011-03-07
    • 2018-09-13
    • 2012-08-10
    • 1970-01-01
    • 2017-06-16
    相关资源
    最近更新 更多