最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to ensure javascript addition instead of string concatenation (Not always adding integers) - Stack Overflow

programmeradmin1浏览0评论

Through my javascript library, I end up with a string that represents a number. Now I want to preform an addition on that number without it doing a string concatenation instead. The solution is easy ( How do I add an integer value with javascript (jquery) to a value that's returning a string?, How to make an addition instead of a concatenation) if your number is always in integer. However my string could be a float or an integer, and at the time of the addition, and I don't know which it will be. Is there a way to make sure the addition happens regardless of whether it's a float or integer?

Through my javascript library, I end up with a string that represents a number. Now I want to preform an addition on that number without it doing a string concatenation instead. The solution is easy ( How do I add an integer value with javascript (jquery) to a value that's returning a string?, How to make an addition instead of a concatenation) if your number is always in integer. However my string could be a float or an integer, and at the time of the addition, and I don't know which it will be. Is there a way to make sure the addition happens regardless of whether it's a float or integer?

Share Improve this question edited May 23, 2017 at 12:23 CommunityBot 11 silver badge asked Oct 19, 2012 at 20:39 dnc253dnc253 40.3k41 gold badges144 silver badges160 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 10

Try using parseFloat. The input variables will be converted to floats, which will work whether the string contains an integer or a float. Your result will always be a float.

It is reasonably safe to always use parseFloat even if you are given an integer. If you must use the appropriate conversion, you could match for a decimal point and use the appropriate function.

I'm going to assume that float addition is sufficient, and if it isn't, then you'll need to dig deeper.

function alwaysAddAsNumbers(numberOne, numberTwo){
  var parseOne = parseFloat(numberOne),
      parseTwo = parseFloat(numberTwo);
  if (isNaN(parseOne)) parseOne = 0;
  if (isNaN(parseTwo)) parseTwo = 0;
  return parseOne + parseTwo;
}

To take what @Asad said, you might want to do this instead:

function alwaysAddAsNumbers(a, b){
  var m = 0,
      n = 0,
      d = /\./,
      f = parseFloat,
      i = parseInt,
      t = isNaN,
      r = 10;
  m = (d.test(a)) ? f(a) : i(a,r);
  n = (d.test(b)) ? f(b) : i(b,r);
  if (t(m)) m = 0;
  if (t(n)) n = 0;
  return m + n;
}

this will always give you at least a zero output, and doesn't tell you if either one is NaN.

For some reason.... who knows???

Using parseFloat works.... when parseInt does not... obviously they are not always the same.

FWIW.... I'm "adding" data elements from "checked" checkboxes here...

    var AddOns = 0;

    $( '.broadcast-channels').each(function(){              
        if ( $(this).prop("checked")) {                             
            var thisAddOn =  parseFloat($(this).data('channel'));      
            AddOns = AddOns + thisAddOn;                                      
        }           
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论