473,769 Members | 3,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(v alor/5)){
valor=valor+1;
}
if (valor/5!=Math.floor(v alor/5)){
valor=valor+1;
}
if (valor/5!=Math.floor(v alor/5)){
valor=valor+1;
}
if (valor/5!=Math.floor(v alor/5)){
valor=valor+1;
}
if (valor/5!=Math.floor(v alor/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.pv p1.value)*(docu ment.f1.u1.valu e));

any help would be welcome, sorry for my poor english and greetings
from spain
Jul 23 '05 #1
2 2552
On 14 Dec 2004 03:26:28 -0800, SoLRaC <so*******@gmai l.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(1234 56, 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.rou nd(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.pv p1.value)*(docu ment.f1.u1.valu e));


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*******@gmai l.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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
5648
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
8776
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 Math.ceil/floor and parseInt +/- 1 to ensure the amount to move was at least 1 and in the right direction. I made a small test to see which one is faster and also included simply adding/subtracting 1. parseInt generally took 50% to 100% longer than...
6
2613
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 3.21 will display 3.21
20
2337
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
3446
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 = 526.5 CType(Math.Round(ldecWater, 0), String) -- Chris Davoli
14
4670
by: yansong1990 | last post by:
Uhm..it's my 1st time using this fuction...could someone provide anexample for me please? thanks
13
537
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): undefined reference to `ceil' collect2: ld returned 1 exit status
7
3780
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) (header.width / 8)) * 4; For example, when header.width is 1790, this code returns 892, when it should be returning 896...
7
25673
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 wrote my codes, but somehow it didn't give the right answer if the number is zero. My simple code looks like this: #include <math.h> int main(){ printf("The nearest integer from %f is %f\n", 0,ceil(0) ); }
0
9590
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10223
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.