# Rounding Input Value ??

Bikash Paul

Ranch Hand

Posts: 342

posted 12 years ago

Hi all,

I am trying to write one javascript function which round the input value(price of product).And all factor on which this rounding will be done that all those factors are coming from database.And in JSP we are storing it in session as a Value object.Now rounding factors are :-

For example :-

Input value : 12345.2345

Range : 10000 - 99999

Rounding Value : 2

if at position 2(index of Input value) that char is < 4 then

rounded value should : 12300

if at position 2(index of Input value) that char is > 4 then

rounded value should be : 12350

And if Rounding Value : -2

then rounded value should be : 12350.23

And if Rounding Value : -3

then rounded value should be : 12350.235

What I have to do in my function is that what ever price i got from another function in JSP first i have to check the range in which range it will fall depend on that it is defined on which index number of Input value i have to round the value and return that value.Can any one please guide me how i can do that.

Below r my codes which i have tried to write:-

Thanks & Regards

Bikash

[ January 14, 2004: Message edited by: Bikash Paul ]

I am trying to write one javascript function which round the input value(price of product).And all factor on which this rounding will be done that all those factors are coming from database.And in JSP we are storing it in session as a Value object.Now rounding factors are :-

For example :-

Input value : 12345.2345

Range : 10000 - 99999

Rounding Value : 2

if at position 2(index of Input value) that char is < 4 then

rounded value should : 12300

if at position 2(index of Input value) that char is > 4 then

rounded value should be : 12350

And if Rounding Value : -2

then rounded value should be : 12350.23

And if Rounding Value : -3

then rounded value should be : 12350.235

What I have to do in my function is that what ever price i got from another function in JSP first i have to check the range in which range it will fall depend on that it is defined on which index number of Input value i have to round the value and return that value.Can any one please guide me how i can do that.

Below r my codes which i have tried to write:-

Thanks & Regards

Bikash

[ January 14, 2004: Message edited by: Bikash Paul ]

Eric Pascarello

author

Rancher

Rancher

Posts: 15385

6

Eric Pascarello

author

Rancher

Rancher

Posts: 15385

6

Bikash Paul

Ranch Hand

Posts: 342

posted 12 years ago

Hi Eric,

First of all thanks for ur reply.

That i have done a mistake in postion of that number(12345.2345) in my example

if at position 1(index of Input value) that char is > 4 then

rounded value should be : 12350

It was my typo mistake.

And if Rounding Value : -2

then rounded value should be : 12345.23

And if Rounding Value : -3

then rounded value should be : 12345.235

Any way your function is perfectly match with my requirement.

I have modified ur code as per my requirement.

But only question is i want that my function should return that rounded value after rounding so that i can paste that ronded value on textfield of my UI(User Interface) Screen.

Below r my modified codes.Can u please check is it work as per my requirement.

Thanks & Regards

Bikash

[ January 14, 2004: Message edited by: Bikash Paul ]

First of all thanks for ur reply.

**If the number is greater than 4 then you leave it alone??**That i have done a mistake in postion of that number(12345.2345) in my example

if at position 1(index of Input value) that char is > 4 then

rounded value should be : 12350

**Also you messed up the numbers when you are showing you examples....**It was my typo mistake.

And if Rounding Value : -2

then rounded value should be : 12345.23

And if Rounding Value : -3

then rounded value should be : 12345.235

Any way your function is perfectly match with my requirement.

I have modified ur code as per my requirement.

But only question is i want that my function should return that rounded value after rounding so that i can paste that ronded value on textfield of my UI(User Interface) Screen.

Below r my modified codes.Can u please check is it work as per my requirement.

Thanks & Regards

Bikash

[ January 14, 2004: Message edited by: Bikash Paul ]

Eric Pascarello

author

Rancher

Rancher

Posts: 15385

6

Bikash Paul

Ranch Hand

Posts: 342

posted 12 years ago

Hi Eric,

First of all thanks.

Your prevoius function was correct.With recent one there is a problem.

If I set:-

minNumber = 0;

maxNumber = 99999;

And in TestIt function :-

function TestIt()

{

//Rounded Value should be 12346 but it is giving 12345.99(wrong value)

alert(RoundNumber(12345.99898989,-2))

//Rounded Value should be 12400 but it is giving 12300(wrong value)

alert(RoundNumber(12355.12345,2))

//Rounded Value should be 12345.24 but it is giving 12345.23(wrong value)

alert(RoundNumber(12345.2355,-2))

}

Another Question about array in javascript in my getRondedValue function i have declared some array variable and iam taking the value in array.It's a Jsp page.

function getRoundedValue(theNum)

{

roundPlace = new Array();

minNumber = new Array();

maxNumber = new Array();

<%

if((ValueObject[])session.getAttribute("RoundData")!=null)

{

ValueObject[] roundingData = (ValueObject[])session.getAttribute("RoundData");

PrcRoundingVO vo = null; //Value object

for(int i = 0;i<=roundingData.length-i;i++)

{

//casting array in Value object

vo = (PrcRoundingVO)roundingData[i]; %>

//Here Iam taking the value in array

roundPlace[<%=i%>] = '<%=vo.getRounding_val()%>';

minNumber[<%=i%>] = '<%=vo.getRounding_min()%>';

maxNumber[<%=i%>] ='<%=vo.getRounding_max()%>';

//My question is here should I set array variable in if condition like below

//Is it work ?

if(minNumber != null && maxNumber != null)

{

//Check to see if number is in range

if(theNum >= minNumber && theNum <= maxNumber)

{

RoundNumber(theNum,roundPlace);

}

else{

alert('Number is not in input range')

return false;

}

}

<%}

}%>

Thanks & Regards

Bikash

[ January 17, 2004: Message edited by: Bikash Paul ]

First of all thanks.

Your prevoius function was correct.With recent one there is a problem.

If I set:-

minNumber = 0;

maxNumber = 99999;

And in TestIt function :-

function TestIt()

{

//Rounded Value should be 12346 but it is giving 12345.99(wrong value)

alert(RoundNumber(12345.99898989,-2))

//Rounded Value should be 12400 but it is giving 12300(wrong value)

alert(RoundNumber(12355.12345,2))

//Rounded Value should be 12345.24 but it is giving 12345.23(wrong value)

alert(RoundNumber(12345.2355,-2))

}

Another Question about array in javascript in my getRondedValue function i have declared some array variable and iam taking the value in array.It's a Jsp page.

function getRoundedValue(theNum)

{

roundPlace = new Array();

minNumber = new Array();

maxNumber = new Array();

<%

if((ValueObject[])session.getAttribute("RoundData")!=null)

{

ValueObject[] roundingData = (ValueObject[])session.getAttribute("RoundData");

PrcRoundingVO vo = null; //Value object

for(int i = 0;i<=roundingData.length-i;i++)

{

//casting array in Value object

vo = (PrcRoundingVO)roundingData[i]; %>

//Here Iam taking the value in array

roundPlace[<%=i%>] = '<%=vo.getRounding_val()%>';

minNumber[<%=i%>] = '<%=vo.getRounding_min()%>';

maxNumber[<%=i%>] ='<%=vo.getRounding_max()%>';

//My question is here should I set array variable in if condition like below

//Is it work ?

if(minNumber != null && maxNumber != null)

{

//Check to see if number is in range

if(theNum >= minNumber && theNum <= maxNumber)

{

RoundNumber(theNum,roundPlace);

}

else{

alert('Number is not in input range')

return false;

}

}

<%}

}%>

Thanks & Regards

Bikash

[ January 17, 2004: Message edited by: Bikash Paul ]