javascript - use parseFloat while checking for NULL values - Stack Overflow

In AngularJS, I am trying to figure out how many hours, for a current worker, are standard vs overtime.

In AngularJS, I am trying to figure out how many hours, for a current worker, are standard vs overtime. I have a formula that works well, if the hours are integers, but fails if it has decimals.

I have to account for situations where one of the numbers may not have been provided. This is my current approach:

laborItem.StraightHours = (parseFloat((laborItem.HoursB4Today | 0)) + parseFloat(laborItem.Hours | 0)) < 40 ?
                    (parseFloat(laborItem.Hours | 0)) :
                    40 - parseFloat(laborItem.HoursB4Today | 0);

This returns only the whole hours.

I have also tried:

laborItem.StraightHours = (parseFloat((laborItem.HoursB4Today | 0).toFixed(2)) + parseFloat(laborItem.Hours | 0).toFixed(2)) < 40 ?
                    (parseFloat(laborItem.Hours | 0).toFixed(2)) :
                    40 - parseFloat(laborItem.HoursB4Today | 0).toFixed(2);

But, this just adds two decimals. I need to preserve the decimal values.

How do I modify my formula to get this to work? And, do I really need to use parseFloat at all, or is there another/better way?

In AngularJS, I am trying to figure out how many hours, for a current worker, are standard vs overtime. I have a formula that works well, if the hours are integers, but fails if it has decimals.

I have to account for situations where one of the numbers may not have been provided. This is my current approach:

laborItem.StraightHours = (parseFloat((laborItem.HoursB4Today | 0)) + parseFloat(laborItem.Hours | 0)) < 40 ?
                    (parseFloat(laborItem.Hours | 0)) :
                    40 - parseFloat(laborItem.HoursB4Today | 0);

This returns only the whole hours.

I have also tried:

laborItem.StraightHours = (parseFloat((laborItem.HoursB4Today | 0).toFixed(2)) + parseFloat(laborItem.Hours | 0).toFixed(2)) < 40 ?
                    (parseFloat(laborItem.Hours | 0).toFixed(2)) :
                    40 - parseFloat(laborItem.HoursB4Today | 0).toFixed(2);

But, this just adds two decimals. I need to preserve the decimal values.

How do I modify my formula to get this to work? And, do I really need to use parseFloat at all, or is there another/better way?

Share Improve this question edited Jul 26, 2016 at 18:37 davids asked Jul 25, 2016 at 5:42 davidsdavids 5,59712 gold badges60 silver badges96 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can try the following code

laborItem.HoursB4Today = parseFloat(laborItem.HoursB4Today || 0);
laborItem.Hours = parseFloat(laborItem.Hours || 0);
laborItem.StraightHours = (laborItem.HoursB4Today + laborItem.Hours) < 40 ? laborItem.Hours : 40 - laborItem.HoursB4Today;

I just modified you code little bit but this can ensure that the future usage of the variables (HoursB4Today,Hours) of laborItem gurantees that they are pointing to a numeric values. You don't need to parse them again and again.

I guess you rather want to use || instead of | in e.g. laborItem.HoursB4Today | 0

A single pipe is the bit-wise OR operator and 'converts' your numbers - if existing - into integers. The || is usually used to provide a default value in case the first operand evaluates to false.

So try changing the expressions like

laborItem.HoursB4Today | 0

into

laborItem.HoursB4Today || 0   

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745669255a4639306.html

相关推荐

  • javascript - use parseFloat while checking for NULL values - Stack Overflow

    In AngularJS, I am trying to figure out how many hours, for a current worker, are standard vs overtime.

    21天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信