Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
I've used a fuction on key up event in the script below.
All is working fine but I want to round off the value 'p1' as If value is in between 5.000001 - 5.999999 the result should be 6.

Thanks in advance.

JavaScript
$(function () {
var textBox1 = $('input:text[id$=FreightCharges]').keyup(foo);
var textBox2 = $('input:text[id$=FuelSurcharge]').keyup(foo);
var textBox3 = $('input:text[id$=FOV]').keyup(foo);

function foo() {
var value1 = textBox1.val();
var value2 = textBox2.val();
var value3 = textBox3.val(); 
var p1 = per(value1, value2, value3); 
$('input:text[id$=ServiceTax]').val(p1);
}
function per() {
var sum1 = 0, p1 = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
if (IsNumeric(arguments[i])) {
sum1 += parseInt(arguments[i]);
p1 = parseInt(sum1 * 12) / 100;
}
}
return p1;
}
function IsNumeric(input) {
return (input - 0) == input && input.length > 0;
}

});
Posted
Updated 11-May-15 1:20am
v3
Comments
Mohibur Rashid 11-May-15 4:53am    
change this line
p1 = parseInt(sum1 * 12) / 100;
to
p1 = Math.round(sum1*0.12);//move 0.12 in a variable
or
p1 = Math.ceil(sum1*0.12);//move 0.12 in a variableor
or
p1 = Math.floor(sum1*0.12);//move 0.12 in a variable

There are a number of types of rounding. You wish to "always round up to an integer" not "round off" which implies rounding down.
http://en.wikipedia.org/wiki/Rounding[^]

To do this use ceil():

http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#ceil(double)[^]


so:

JavaScript
p1 = Math.ceil(sum1 * 0.12);
 
Share this answer
 
v4
Comments
Joezer BH 11-May-15 7:27am    
5+
arunsiddhu1992 13-May-15 4:24am    
I got It.
Thanks.:)
Rounded percentage in JS:
JavaScript
function RoundedPct(quantity, percent)
{
    return Math.ceil(quantity * percent / 100);
}
 
Share this answer
 
v3
Comments
[no name] 11-May-15 6:28am    
Round rounds to nearest integer. Thus:
5.000001 gives 5
5.999999 gives 6

Not what the questioner asked for.
Joezer BH 11-May-15 7:19am    
right you are! I missed that one due to the title ... fixed the title, and the A
arunsiddhu1992 13-May-15 4:23am    
Thanks I've got the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900