1

I have the below JavaScript code to round the values, which returns different result in similar cases. This is being called through a tool.

var precision = -1;
        		
        	
        /**
         * Initialization
         */
            function _init() 
            {
        	precision = 8;
            }

case1
            function RoundUp(number, num_digits)
            {
            	var shift = Math.pow(10, num_digits);
            	var rounded = Math.ceil(number * shift) / shift;
                rounded = (precision>-1) ? Number(rounded.toFixed(precision)) : rounded;
                return rounded;
            }
            
case2                
            function RoundUp(110, 1)
            {
            	var shift = Math.pow(10, num_digits);
            	var rounded = Math.ceil(number * shift) / shift;
                rounded = (precision>-1) ? Number(rounded.toFixed(precision)) : rounded;
                return rounded;
            }

If we directly pass some parameter values, ex (110,1), it returns 110. (CASE2) But when we pass the same parameter values indirectly through some variable (CASE1), the output changes to 111 for some reason.

Here, number = 110 and num_digits = 1

I cant figure out why this is the case. Can someone please help. Pardon me, but I am new to JavaScript.

Thanks

2
  • 5
    The second case is not valid format. Values are passed when you call the function, not when you define the function. What is the use of _init? Do you ever call it? Commented Feb 14, 2019 at 21:30
  • Please provide examples that are actually runable for both cases. Commented Feb 14, 2019 at 21:33

1 Answer 1

1

It's not entirely clear what you're actually trying to do. However, you appear to have two problems.

1. Syntax error

This line:

function RoundUp(110, 1)

is not a legal JavaScript statement. You cannot pass values to a function while you're defining it. What JavaScript thinks you're trying to do here is define a function with two parameters called 110 and 1, which are both illegal parameter names, so you get a syntax error.

2. Math

I'm assuming that what you're trying to do is this:

roundUp(12345, 1)  =>  20000
roundUp(12345, 2)  =>  13000
roundUp(12345, 3)  =>  12400
roundUp(12345, 4)  =>  12350  etc

If that's correct, then your math is wrong. Try this instead:

function roundUp(number, numDigits) {
  var shift = Math.pow(10, number.toString().length - numDigits);
  var rounded = Math.ceil(number / shift) * shift;
  rounded = (precision>-1) ? Number(rounded.toFixed(precision)) : rounded;
  return rounded;
}

This will not work for floating-point numbers, but if you only need to deal with integers it's fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.