How could I display 10.00?

M

Man-wai Chang

function caltotal() {
document.theform.txtTotal=10.00;
}

<form name="theform" ...>
<input type=textbox maxlength=10 size=10 name="txtTotal">
<input type=textbox maxlength=10 size=10 name="input" onchange="caltotal()">
</form>

It just truncates the .00 of txtTotal....
 
R

RobG

What's wrong about using toFixed()? It does work at least for the cases
I tested...

It is not reliable, try:

var x = 1.035;
var y = 0.0094;
alert( x.toFixed(2) +
'\n' + y.toFixed(2)
);

In IE you will see 1.04 and 0.00, in other browsers (Firefox, Safari,
Opera at least) you will see 1.03 and 0.01. There are many other
examples.

IE's implementation of toFixed is not compliant with the ECMAScript
specification.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
Sorry, should have mentioned that the code in the above replaces the
native Number.prototype.toFixed method, so if you include it in your
page you can reliably do stuff like:

var price = 12.99;
var tax = 6.5 // 6.5%
var total = price + price*tax/100;
alert( 'Total: ' + total + '\n'
+ 'Rounded: ' + total.toFixed(2) );

However, it is often desirable to put a fixed number of characters
before the decimal point, as well as a fixed number of digits after it.
There's no way toFixed can do that, but the code cited, when not used to
emulate toFixed, has that option built in.


It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
R

RobG

In comp.lang.javascript message <[email protected]




However, it is often desirable to put a fixed number of characters
before the decimal point, as well as a fixed number of digits after it.
There's no way toFixed can do that, but the code cited, when not used to
emulate toFixed, has that option built in.

Many users directed to the FAQ would likely never work out how to use
the code provided, much less discover that the functions can be used
for other purposes. The names of the functions don't help - StrS is
hardly descriptive for a function that formats numbers.

It's a good idea to read the newsgroup c.l.j and its FAQ.

I have no idea whay you include that comment - did I miss something?
See below.

There is nothing in FAQ 4.6 that helps people use the code provided.
Your site provides a vast amount of information but doesn't provide
suitable FAQ-type help. If the FAQ was well worded, there'd be no need
for you to point out that the set of functions in 4.6 can be used for
other purposes.

For example:

function Stretch(Q, L, c) { var S = Q
if (c.length>0) while (S.length<L) { S = c+S }
return S
}

assumes Q and c are strings. To make it a more understandable and a
generic padding function, consider:

function padString( value, newLength, paddingChar)
{
var s = String(value);
var c = String(paddingChar);

if (c.length > 0) {
while (s.length < newLength) {
s = c + s;
}
}
return s;
}

alert( padString('abc', 6, '_')); // Shows ___abc
alert( padString(7, 3, '0')); // Shows 007


I'll offer a suggestion next time that entry is the FAQ post of the
day, it probably deserves its own notes page.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
I have no idea whay you include that comment - did I miss something?

It's in my "javascript" signature file, which is an editable part (if
selected) of the draft reply. In principle I always leave it in if it
applies particularly to one of those quoted in the article, and also
sometimes when it does not, so that others may see it. But it counts as
body, not sig, being before the sigsep. It also serves as a reminder,
for those who might be mis-lead by others, of how "its" and "it's" are
used in the English language.

There is nothing in FAQ 4.6 that helps people use the code provided.
Your site provides a vast amount of information but doesn't provide
suitable FAQ-type help. If the FAQ was well worded, there'd be no need
for you to point out that the set of functions in 4.6 can be used for
other purposes.

That's a matter for the FAQ maintainer. The author of the code in FAQ
4.6 uses somewhat different code now, in which the first parameter is
not assumed or required to be a string and the third is extinct; and the
core routine is also changed. That's been said here before.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top