ceil a number with 2 decimals to 0 or 5

S

SoLRaC

I have a form, and i want with javascript round a number with 2
decimals to de uper next 0 or five.

IE if you have the number 4,53 it must be rounded to 4,55. Until now i
wrote that

var valor
function redondeo(valor){
valor= valor *100;

if (valor/5!=Math.floor(valor/5)){
valor=valor+1;
}
if (valor/5!=Math.floor(valor/5)){
valor=valor+1;
}
if (valor/5!=Math.floor(valor/5)){
valor=valor+1;
}
if (valor/5!=Math.floor(valor/5)){
valor=valor+1;
}
if (valor/5!=Math.floor(valor/5)){
valor=valor+1;
}

valor=valor/100;
}

But when i call to de function it wrote me in the text box "nan" (not
a number i supose)

document.f1.t1.value=redondeo((document.f1.pvp1.value)*(document.f1.u1.value));

any help would be welcome, sorry for my poor english and greetings
from spain
 
M

Michael Winter

I have a form, and i want with javascript round a number with 2
decimals to de uper next 0 or five.

IE if you have the number 4,53 it must be rounded to 4,55. Until now i
wrote that

function round(n) {var lsd;
n *= 100;
lsd = n % 10;
n -= lsd;

if(0 < lsd) {
if(5 > lsd) {
lsd = 5;
} else if(5 < lsd) {
lsd = 0;
}
}
n += lsd;

/* n now contains the original value, as an integer,
* with the least significant digit (LSD) rounded
* up so that n is a multiple of 5.
*/

/* ... */

}

How you end the function depends on how you'll use it.

If this is for currency calculations, or anything where accuracy is
paramount, the value should really stay as an integer, so replace

/* ... */

with

return n;

When you need to output the value later, you can use a function to convert
the number to a decimal string. An example of such a function is

/* Returns a string representing a number in fixed-point
* notation.
*
* n - The number to format.
* p - The number of decimal places already present in n. For
* example, if n was 150 and it represented 1.50, this
* argument should be 2. Defaults to 0.
*
* intToFixed(123456, 2)
* returns 1234.56
*/
function intToFixed(n, p) {
var s = (0 > n) ? '-' : ''; n = Math.abs(n);
var i, f, j, m; p = +p || 0; j = p + 1;

if(0 > p || p > 20) {return;}
if(isNaN(n)) {return 'NaN';}

m = String(Math.round(n));
while(j > m.length) {m = '0' + m;}
f = m.substring((j = m.length - p));
i = m.substring(0, j);
return s + i + (p ? '.' + f : '');
}

Alternatively, replace

/* ... */

with

return n / 100;

if accuracy is not so important.

[snip]
document.f1.t1.value=redondeo((document.f1.pvp1.value)*(document.f1.u1.value));

Before performing arithmetic, you should really consider validating the
contents of those inputs. You should also consider using better
identifiers.

[snip]

Hope that helps,
Mike
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Tue, 14 Dec 2004 03:26:28, seen in
SoLRaC said:
I have a form, and i want with javascript round a number with 2
decimals to de uper next 0 or five.

IE if you have the number 4,53 it must be rounded to 4,55.

x = Math.ceil(x*20)/20 // will do, apart from the separator.

Should 4.56 go to 4.6 or '4.60' ? How about negative inputs ?

If you want to get a string result, see FAQ 4.6, but put M=N=1, include
the above concept, and adapt as needed; or use

StrU(Math.ceil(x*20)/20, 1, 2)

Note that StrU can easily be changed to use a comma.

Note, however, that 4.65, for example cannot be represented exactly; to
be reasonably safe with euros, do all the calculations in cents. Better
to stick to lire.

BTW, test with negative inputs, and with ones like 0.7 & 0.07.

Another way might be to form String(x + 0.055 - x % 0.05) , locate its
decimal point, and chop the string after two characters later :
String(x + 0.055 - x % 0.05).replace(/(\.\d\d).+/, "$1")
UNDERTESTED
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top