如果变量的值为空,则获取NaN [英] Getting NaN if variable has null value

查看:162
本文介绍了如果变量的值为空,则获取NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我正在通过ajax和json进行记录. 如果变量具有值,我将获得价值. 但是,如果变量性能" 具有空值,则显示NaN. 我想打印数字值而不是NaN.

Here, I am getting record through ajax and json. I am getting value if the variable have. But, If variable "performance" have null value, it shows NaN. Instead of NaN, I want to print number value like, 00.00.

有可能吗?如果是,那怎么办? 谢谢.

Is that possible? If yes then how? Thank you.

我的代码,

function emp_month_perf(){

            jQuery.ajax({
                url: "<?php echo base_url(); ?>grade_tasks/emp_monthly_performance",
                data:'',
                type:"GET",
                dataType: "json",
                success:function(data){

                    var total_month_earn = data.total_earn_point;
                    var total_month_point = data.total_point;
                    var performance;
                    var per_color;
                    //var radius = "20px";
                    //alert(total_point);
                    performance = (((total_month_earn)/(total_month_point))*100).toFixed(2);
                    if(performance>80)
                    {
                        per_color = "#33CF53";
                    }
                    else if(performance>=60 && performance<=80)
                    {
                        per_color = "#E0C533";
                    }
                    else
                    {
                        per_color = "#E12827";
                    }
                    //document.getElementById("monthperformance").style.borderRadius = radius;
                    document.getElementById("monthperformance").style.backgroundColor = per_color;
                    $('#monthperformance').html(performance);
                },
                error:function (){}
                });
                }
               setInterval(emp_month_perf, 300000);

推荐答案

使用OR运算符将数字设置为零.

Use an OR operator to set the number to zero.

var total_month_earn = data.total_earn_point || 0;
var total_month_point = data.total_point || 0;

但是现在您可以将1/0设为无穷大. :)

But now you can have 1/0 which would be infinity. :)

其他选项是检查NaN,然后​​将该值设置为零.

Other option is to Check for NaN and than set the value to zero.

var performance = (((total_month_earn)/(total_month_point))*100);
var formatted = isNaN(performance) ? "00.00" : performance.toString(2); 

这篇关于如果变量的值为空,则获取NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆