473,394 Members | 1,841 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,394 software developers and data experts.

FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2008-11-12)

-----------------------------------------------------------------------
FAQ Topic - How do I format a Number as a String with
exactly 2 decimal places?
-----------------------------------------------------------------------

When formatting money for example, to format 6.57634 to
6.58, 6.5 to 6.50, and 6 to 6.00?

Rounding of x.xx5 is uncertain, as such numbers are not
represented exactly. See the section on "simple decimal arithmetic"
for Rounding issues.

` N = Math.round(N*100)/100 ` only converts N to a Number of value
close to a multiple of 0.01; but ` document.write(N) ` does not give
trailing zeroes.

ECMAScript Ed. 3.0 (JScript 5.5 [but buggy] and JavaScript 1.5)
introduced ` Number.prototype.toFixed `, the main problem
with this is the bugs in JScript's implementation.

Most implementations fail with certain numbers, for example 0.07.
The following works successfully for ` M>0, N>0: `

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

function StrU(X, M, N) { // X>=0.0
var T, S = String(Math.round(X * Number("1e" + N)));
if (S.search && S.search(/\D/) != -1) {
return '' + X;
}
with (String(Stretch(S, M+N, '0')))
return substring(0, T = (length-N)) + '.' + substring(T);
}

function Sign(X) {
return X 0 ? "+" : X < 0 ? "-" : " ";
}

function StrS(X, M, N) {
return Sign(X) + StrU(Math.abs(X), M, N);
}

Number.prototype.toFixed = function(n){
return StrS(this, 1, n);
};

http://www.merlyn.demon.co.uk/js-round.htm

http://msdn2.microsoft.com/en-us/library/sstyff0z.aspx
--
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers. The sendings of these
daily posts are proficiently hosted by http://www.pair.com.

Nov 12 '08 #1
4 2757
What about multiplying by 100
and then cast (int) onto it,
then cast double (or whatever you need)
and divide by 100?

so
x = 6.43211
100x = 643.211
then (int) 643.211 = 643
then (double) 643 = 643
then / 100 = 6.43

Nov 12 '08 #2
What about multiplying by 100
and then cast (int) onto it,
then cast double (or whatever you need)
and divide by 100?

so
x = 6.43211
100x = 643.211
then (int) 643.211 = 643
then (double) 643 = 643
then / 100 = 6.43

Nov 12 '08 #3
disappearedng wrote:
What about multiplying by 100
and then cast (int) onto it,
then cast double (or whatever you need)
and divide by 100?
<http://jibbering.com/faq/#appropriateQuestions>
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Nov 12 '08 #4
In comp.lang.javascript message <2d662260-bd2e-46f2-bd3f-df7641fd7416@q3
0g2000prq.googlegroups.com>, Tue, 11 Nov 2008 17:40:00, disappearedng
<di***********@gmail.composted:
>What about multiplying by 100
and then cast (int) onto it,
then cast double (or whatever you need)
and divide by 100?

so
x = 6.43211
100x = 643.211
then (int) 643.211 = 643
then (double) 643 = 643
then / 100 = 6.43
What language might you be talking about there? If x is 6.4007, then
where are your two decimal places; if x=6.9997, where are the decimal
places? Where, on fact, is your String, as per Subject? Did you
actually understand the Subject?

All that your code can achieve is to convert a Number into one which is,
as nearly as possible, a multiple of 1/100.

FAQ Topic -

Because the FORTRAN convention is commonly, though not everywhere, used,
the identifier N in the paragraph starting "` N = Math.round(N*100)/100"
should be changed to be, say, X in 4 places.

In the Subject, 2 should be changed to N.

There is better code via the first link.

The FAQ does not say that, in StrU, N is the number of decimal places,
and M is the number of non-sign characters before the decimal point.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Nov 12 '08 #5

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

Similar topics

3
by: Brent Bortnick | last post by:
Does anyone know how to find out the number of decimal places a number has. I need this info so that i can round to 3 decimal places if the number has 3 decimal places or to 2 decimal places if...
1
by: Gizmo | last post by:
Hi there i was wondering if any one new to a function that rounds up a float to so many decimal places. I have a number in bytes and converting it to mb's and gb's but once its converted i need to...
3
by: Dean G | last post by:
Hi, I need to truncate a number to two decimal places without rounding. All the functions i;ve tried tend to round up the numbers. Also i cant use any string functions to limit the size because the...
7
by: BA | last post by:
Hello, I have a string with a price in: "$14.95" I need to get it into a decimal. So I regex 'd the string and dumped the $. Debug mon shows a clean string "14.95" Then I do a...
27
by: Gaijinco | last post by:
Sooner or later everytime I found recreational programming challenges I stumble with how I test if a number is has decimal places differnt than 0? For example if I want to know if a number is a...
3
by: Sandy | last post by:
Hello I need to have a value display in a label with two decimal places; e.g. "4" to display as "4.00 Any help will be appreciated Sandy
2
by: Andy | last post by:
Hi I'm really stuck outputting a double number to the console with three decimal places if the furthest right value is a zero. I can coutput the number 4.546 as 4.546 but then if I output...
3
by: paulquinlan100 | last post by:
Hi I have a couple of calculated fields in a report, i've set the Decimal Places property to 0 and the format to General Number, however when previewing the report it still comes up with a...
1
by: jesmi | last post by:
hi!! i want to place two fix decimal places after a digit even if there is no decimal places after a digit. for example if there is a number 5, i want output 5.00 like that. please help me. ...
1
by: Serenityquinn15 | last post by:
Hi! I have a little trouble about in truncating numbers. What i want to do is to get the two decimal places. here's the example: when i try to get the half of 20500.39 it shows like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.