473,771 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing formatted decimal numbers

I need to print formatted decimal numbers to a set number of decimal places.

Is there a way to do this with JavaScript ?

If not is there any existing/"library" functions to do this ?

Many thanks in advance,

Aaron
Jun 16 '06 #1
3 2324

Aaron Gray wrote:
I need to print formatted decimal numbers to a set number of decimal places.

Is there a way to do this with JavaScript ?

If not is there any existing/"library" functions to do this ?

Many thanks in advance,

Aaron


pi = 3.14159265;
x = n.toFixed(2);
// x == 3.14
y = n.toFixed(4);
// y == 3.1416

Note that this is only in JS1.5 and ECMAv3, but that is still most
everything released this century.

Jun 17 '06 #2
Joshie Surber said the following on 6/16/2006 9:04 PM:
Aaron Gray wrote:
I need to print formatted decimal numbers to a set number of decimal places.

Is there a way to do this with JavaScript ?

If not is there any existing/"library" functions to do this ?

Many thanks in advance,

Aaron


pi = 3.14159265;
x = n.toFixed(2);
// x == 3.14
y = n.toFixed(4);
// y == 3.1416

Note that this is only in JS1.5 and ECMAv3, but that is still most
everything released this century.


Also note that toFixed() is severely crippled in IE with it's bug. Read
the FAQ with regards to formatting a number with leading and/or trailing
zeroes.

Convert it to a string, pad it, print the string.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jun 17 '06 #3
JRS: In article <4f************ *@individual.ne t>, dated Fri, 16 Jun
2006 21:46:09 remote, seen in news:comp.lang. javascript, Aaron Gray
<an********@gma il.com> posted :
I need to print formatted decimal numbers to a set number of decimal places.

Is there a way to do this with JavaScript ?

If not is there any existing/"library" functions to do this ?


Read the newsgroup FAQ, and <URL:http://www.merlyn.demon.co.uk/js-
round.htm> ff.

--
© 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.
Jun 18 '06 #4

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

Similar topics

21
4534
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
17
6150
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number. "Decimal Number" sometimes serves to distinguish Base 10 numbers, eg "15", from Base 2 numbers, Eg "1111". At other times "Decimal Number" serves to differentiate a number from an integer. For the rest of this post I shall only use either...
2
12550
by: SamIAm | last post by:
I am displaying money values in a textbox like this: txtCountryPack.Text = String.Format("{0:c}", dr); I then need to updated my Database: decimal packPrice = Convert.ToDecimal(txtCountryPack.Text.Trim()); The problem is that the value is in a formatted format i.e. $300.00 How do I remove the formatting with have to do a SubString? Cheers.
10
8150
by: Paul Sullivan | last post by:
decimal d; d = 1.1M OR d= (decimal) 1.1 Discussioon
28
5932
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into 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 section 4.7 for Rounding issues.
9
7715
by: NEWSGROUPS | last post by:
I have data in a table in an Access 2000 database that needs to be exported to a formatted text file. For instance, the first field is an account number that is formatted in the table as text and is 8 characters long. This field needs to be exported as pic(15) padded in the front with 0's (zeros). The next field an ID name that is 15 characters that needs to be exported as pic(20) padded with trailing spaces. There are about 5 fields in...
2
1850
by: utab | last post by:
Dear all, On a formatted printing operation that I am trying to accomplish for doubles, I have a question. I would like to print a double, d, and its square, d*d, but I would like to make the argument to be computed upon the length of the numbers(d and d*d) so that it lines up neatly in the columns. For this operation, I need the number of digits in a double, however, due to floating point representation, the double values are not...
10
5326
by: Jason | last post by:
I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole number text box, a numerator text box, and a denominator text box. I realize that this is probably the closest I'll get to having fractions displayed in VB, so that's no big deal. I'm able to simplify most numbers with some code I've written,...
0
28067
Frinavale
by: Frinavale | last post by:
Convert a Hex number into a decimal number and a decimal number to Hex number This is a very simple script that converts decimal numbers into hex values and hex values into decimal numbers. The example following this one demonstrates how to convert decimal numbers into binary values. A hex number is a string that contains numbers between 0-9 and characters A-F; where A = 10, B = 11, ..., F = 15. (For more information please look up...
0
9619
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
10260
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
10102
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9910
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...
1
7460
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
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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.