5.1 How do I format a Number as a String with exactly 2 decimal places?

D

Dr J R Stockton

Sun said:
What does StrC mean? I was guessing would mean "convert to a signed
numerical string."

The /Str./ identifies it as a member of the set. The "C" can be taken
to mean Common, Core, or Code. It was not intended to be used directly,
only by StrU and StrS. StrU is Unsigned, StrS is Signed, and you can
think of T & W as meaning Thickness & Width, or just as nearby letters.

The variable names are also unclear to me.

M & N are given in comment. St is fairly obviously a String. B was
going to be a Boolean, but it turned out to be a Number either 0 or 1;
one can use false or true instead.

Also, as a convention, variables are camel case, as are function names
(unless the function is intended to be used as a constructor).

I write code according to habits developed in using Algol, SIR, FORTRAN,
Pascal, Delphi, Macro-11, and possibly some I've forgotten, since time
immemorial. It's too late to change them now, but users can alter their
copies as they like.

The FAQ mostly follows that convention. The code could be more explicit
with more descriptive variable and function names, such as
'prependString', 'toUnsignedString', 'toFixedString'.

What do you think?

Bloat. These are short routines, so it should be easy to see what the
variables are used for. I like compact code, so that I can see more at
once; and long names, especially in a case-dependent language with
implicit declaration, are too susceptible to mis-spellings.

It's the algorithm which is important, not the presentation.



While you're there : today I wrote

Date.prototype.toSt = new Function(" /* Omit parenthetical */\n" +
" return this.toString().replace(/( \\(.+\\))$/, '')")

ISTR that there are nicer-looking ways of doing that, apart from using a
lexically-separate function? In particular, I'd like the actual source
to look more like Date.prototype.toSt.toString(), without double
escapes. It removes the wasteful and incorrect bit of FireFox and
Chrome output.


Your newsreader seems to remove blank lines, alas.


It appears that you are a Mac user - I've never seen my site in Mac, and
can only hope it looks much the same as on a PC.
 
D

dhtml

Dr said:
Bloat. These are short routines, so it should be easy to see what the
variables are used for. I like compact code, so that I can see more at
once; and long names, especially in a case-dependent language with
implicit declaration, are too susceptible to mis-spellings.

It's the algorithm which is important, not the presentation.

The fact that the code works is the most important thing.

I think the understanding of how to format a number as a string is
important -- it gets at the essence of the question: "How...".

Explicit variable names and function names helps humans understand what
the code does.

Variable names should be chosen by what the variable is used for, not by
by their download speed.

Code that does not rely on automatic semicolon insertion can be
optimized with minification/compression tools (though minification has
been the subject of disagreement here).

I understand that the thinking behind the algorithm is influenced from
algorithms in Algol, SIR, et c. However, I think the FAQ should use
javascript code conventions.

To me, this:

function toFixedString(n, digits) {
var unsigned = toUnsignedString(Math.abs(n), digits);
return (n < 0 ? "-" : "") + unsigned;
}

- is clearer than:

function StrS(X, M, N) { return StrC(X, M, N, 1) }

That's a bit out of context. The whole code should be judged, and by
more than its contributors. So far there is your code, the FAQ code
(which I believe you wrote) and my modified version of your code, which
appeared in my second post.

Garrett
 

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,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top