473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ceil a number with 2 decimals to 0 or 5

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.va lue)*(document.f1.u1.value));

any help would be welcome, sorry for my poor english and greetings
from spain
Jul 23 '05 #1
2 2528
On 14 Dec 2004 03:26:28 -0800, SoLRaC <so*******@gmail.com> wrote:
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.va lue)*(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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
JRS: In article <49**************************@posting.google.com >,
dated Tue, 14 Dec 2004 03:26:28, seen in news:comp.lang.javascript,
SoLRaC <so*******@gmail.com> posted :
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

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Oswald | last post by:
Hey, how can I set the number of decimals? Example: 5 decimals 2.12345 2 decimals 2.12 I've found something about NumberFormat in the API, but without an example I can't solve this...
6
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
20
by: mikael.liljeroth | last post by:
Why do I get NO as result from this ?? double du = (double)3.1415; du = ceil(du); if(du == (double)4) printf("YES\n"); else printf("NO\n");
4
by: Chris Davoli | last post by:
The folllowing will round to 526, but it should round to 527. It works correctly for all other numbers, except for this one. Does anybody know of a bug in Math.Round? Dim ldecWater As Decimal =...
14
by: yansong1990 | last post by:
Uhm..it's my 1st time using this fuction...could someone provide anexample for me please? thanks
13
by: ptn | last post by:
Hi everyone, I was messing around with math.h and I got this error: """ /tmp/ccefZYYN.o: In function `digcount': itos.c:(.text+0x103): undefined reference to `log10' itos.c:(.text+0x111):...
7
by: MrIncognito | last post by:
Am I using this correctly? This line of code is supposed to find the number of bytes in one line of a 16 color bitmap, but it doesn't seem to work most of the time. long linesize = ceil((double)...
7
by: mingke | last post by:
I've learned that we can round up and down a float number by using Ceil and floor function. But, is this function still able to work if I the number I want to round up (or down) is zero? I have...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.