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

writing a toFixed function

I'm not that familiar with javascript, and I am trying to write a
toFixed function.

The function takes two arguments, the number and the number of decimal
places we're interested in.

If the number of decimal places is negative, it's interpreted as the
number of significant digits.

Thus:

toFix(12345.6789,2) -12345.67
toFix(12345.6789,0) -12345
toFix(12345.6789,-2) -12000

I've hacked up a solution that so far works on all tested browsers for
the first 2 cases.

It's the last one I'm having trouble with... How do I get only the
significant digits?

It has to work for all of the following cases:
toFix(12345.6789,-2) -12000
toFix(12345.6789,-5) -12345
toFix(12345.6789,-7) -12345.67

Thanks,

--Yan
Jul 20 '07 #1
2 2422
In comp.lang.javascript message <13*************@corp.supernews.com>,
Fri, 20 Jul 2007 10:41:52, CptDondo <ya*@NsOeSiPnAeMr.composted:
>I'm not that familiar with javascript, and I am trying to write a
toFixed function.
There is already a toFixed method, available in many browsers.
>The function takes two arguments, the number and the number of decimal
places we're interested in.

If the number of decimal places is negative, it's interpreted as the
number of significant digits.
That's using one function for two different jobs, depending on the sign
of the parameter. ISTM better to use two functions.

Such functions should return strings.

Did you consult the FAQ and the Web pages that it links to?
>--Yan
DSS.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
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.
Jul 20 '07 #2
Dr J R Stockton wrote:
In comp.lang.javascript message <13*************@corp.supernews.com>,
Fri, 20 Jul 2007 10:41:52, CptDondo <ya*@NsOeSiPnAeMr.composted:
>I'm not that familiar with javascript, and I am trying to write a
toFixed function.

There is already a toFixed method, available in many browsers.
>The function takes two arguments, the number and the number of decimal
places we're interested in.

If the number of decimal places is negative, it's interpreted as the
number of significant digits.

That's using one function for two different jobs, depending on the sign
of the parameter. ISTM better to use two functions.

Such functions should return strings.

Did you consult the FAQ and the Web pages that it links to?
Ugh...

I should have engaged brain. We originally dumped the toFix and
toPrecision because we were working with an embedded browser which had
broken implementations. We've since dumped that browser, so I no longer
need to code around its "features".

--Yan
Jul 23 '07 #3

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

Similar topics

12
by: BC | last post by:
I have read the FAQ and the discussions but I am not a Javascript programmer and do not know how to make toFixed (and other techniques) work, after several attempts. The following calculations...
4
by: Mark Kamoski | last post by:
Hi Everyone. What is the real difference between writing a Sub versus writing a Function with no return value? It seems to me that both of these would need to compile to the same IL, so it...
5
by: glegipon | last post by:
As Tom Cahill would require, my challenge is (according to the "three R's") Reproducible: if run on OS 9.x, the monthly payment does not show up in the text box: Recognizable: I believe the...
6
by: Adam Honek | last post by:
Can anyone spot why the below function is failing? It returns true but when looking inside the INI file there's no new added section. ****CODE**** Public Function WriteINIFileSection(ByVal...
1
by: VK | last post by:
Anyone could tell what result is displayed for alert(1.035.toFixed(2)) on Safari 2.0.4 ?
1
by: projectVBA | last post by:
Hi , I wrote some add in for PowerPoint and i'm trying to find an automatic way to load it every time PowerPoint starts. I found Microsoft Code example (see below ) BUT the problem is : I can't...
1
by: Smita Prathyusha | last post by:
I am facing a problem in writing to COM1. I am using a Win 32 Console mode Program in VC++ the following is the code: If anyone can help me out it will be of great help : // SC_Using_Classes.cpp...
1
by: pmactdot | last post by:
Hi, I'm trying to get the below ShowVal() case study to dispaly/return a value to 2 decimals. Where/how would I use toFixed(2) instead or Math.round? function ShowVal() { var...
15
colinod
by: colinod | last post by:
Hi I have the following code and it works fine if i want the total to be in a form field but i want to write the amount to the webpage using document.write, have tried all sorts but cant get it to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.