UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

874 lines (658 loc) 25.6 kB
<!-- Generated by Rakefile:build --> <strong> Daniel </strong> on 2012-09-27 20:43:52 <br /> how add a hundred separator? <hr /> <strong> Vishwa </strong> on 2012-07-17 06:36:10 <br /> Thanks. Works like a charm. <hr /> <strong> <a href="http://hitbiz.net" rel="nofollow">Haider Abbas</a> </strong> on 2012-07-10 21:19:07 <br /> Thanks a lot! Works just like PHP equivalent. Perfect work. Very helpful. <hr /> <strong> Nima </strong> on 2012-01-07 22:28:41 <br /> Thanks , grate job ;) <hr /> <strong> <a href="-" rel="nofollow">Koon</a> </strong> on 2011-09-29 09:40:56 <br /> Thank you so mush !! <hr /> <strong> Memo </strong> on 2011-09-28 01:29:13 <br /> Me salvo la vida xD This save my life xD <hr /> <strong> Sebastian Haller </strong> on 2011-08-31 19:46:54 <br /> Sorry, my fix did not work, it should be <pre><code>s = (prec ? toFixedFix(Math.abs(n), prec) : '' + Math.round(Math.abs(n))).split('.');</code></pre> and <pre><code>return (n&lt;0 ? '-' : '')+s.join(dec);</code></pre> <hr /> <strong> Sebastian Haller </strong> on 2011-08-31 17:25:22 <br /> number_format(-5.5) returns -6 in PHP (because PHP rounds half numbers up = away from zero, while JavaScript rounds them always up). Hence I suggest changing line 66 to <pre><code> s = ((n&lt;0 ? '-' : '') + (prec ? toFixedFix(Math.abs(n), prec) : '' + Math.round(Math.abs(n)))).split('.'); </code></pre> and adding example 14: <pre><code> // * example 14: number_format('-5.5'); // * returns 14: '-6' </code></pre> <hr /> <strong> Fabricio </strong> on 2011-08-02 16:48:23 <br /> Ótimo, foi de grande utilidade, alias, todas as funções são úteis. <hr /> <strong> Theriault </strong> on 2011-03-04 02:15:46 <br /> @WoofWoof: This bug should now be fixed. The new source is available on GitHub, or https://github.com/kvz/phpjs/raw/master/functions/strings/number_format.js <hr /> <strong> <a href="http://www.motor.es" rel="nofollow">motor</a> </strong> on 2011-02-24 12:34:02 <br /> Very well. Thanks you. <hr /> <strong> <a href="http://motorimpress.com/bmw" rel="nofollow">bmw sports</a> </strong> on 2010-12-22 00:43:46 <br /> This codes makes me dizzy :) <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-12-15 05:39:34 <br /> @WoofWoof: It does look like a bug. I'm a little busy at the moment unless someone else can help, but in the meantime, you might try money_format() as it can handle larger numbers. <hr /> <strong> <a href="woofwoof75.com" rel="nofollow">WoofWoof</a> </strong> on 2010-12-15 03:48:41 <br /> Nice one! But I have problem when there's 1,000,000.00... it'll return 0, it is unable to handle such a big sum? only 1000,000.00? I'm relying on this to format $$$... but when dealing with Indonesian Rupiah... which usually of very huge sum. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2010-09-08 16:45:06 <br /> @ Amirouche: Fixed: https://github.com/kvz/phpjs/commit/3f791fd7fa196ba6a1db6c67c6e8c6f24fc0659b <hr /> <strong> Amirouche </strong> on 2010-07-28 14:47:56 <br /> I can't have this: number_en = number_format(1 000,50, 2, '.', ' '); result: number_en = 1000.50 <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2010-06-19 16:25:13 <br /> @ Rafael Martínez: Thanks for taking the time to investigate further. Appreciated <hr /> <strong> <a href="http://www.gestiondecompras.com" rel="nofollow"></a> </strong> on 2010-06-01 16:13:54 <br /> OK, I´m wrong, because, in PHP, the result gives us 555,42 to in number_format(555.425, 2, ',', '.'). I was looking a solution to round like excel. Anyway my &quot;solution&quot; works fine for &quot;excel´s round&quot;. Greets. <hr /> <strong> <a href="http://www.gestiondecompras.com" rel="nofollow"></a> </strong> on 2010-06-01 12:26:32 <br /> One posible solution. Add <pre><code> var ajuste = n + 1/(k*10); </code></pre> and replace the return value for this: <pre><code> return '' + Math.round(ajuste * k) / k; </code></pre> in the function toFixedFix declared over the line 50, leave it something like that: <pre><code> toFixedFix = function (n, prec) { var k = Math.pow(10, prec); var ajuste = n + 1/(k*10); return '' + Math.round(ajuste * k) / k; }; </code></pre> Any suggestion?? <hr /> <strong> <a href="http://www.gestiondecompras.com" rel="nofollow"></a> </strong> on 2010-06-01 09:11:17 <br /> Hi guys, I think that I found a bug because I get 555,42 in that function number_format(555.425, 2, ',', '.') when the result could be 555,43 Any solution? Thanks. Sorry for my english <hr /> <strong> Graci Granados </strong> on 2010-04-24 16:51:00 <br /> Thanks Brother, congratulation!!! <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-01-08 04:17:45 <br /> @Ronnie: We're really just focused here on implementing the already-existing API of PHP, and PHP doesn't have any formal way of converting back to a numeric format. Why don't you just transmit both forms together within your application? Or are you depending on user input which may include use of commas, etc.? If the latter, you might be able to use this: <pre><code>function strtonumber( str, dec_point, thousands_sep ) { // + based on: http://www.php.net/manual/en/function.number-format.php#76448 // + adapted by: Brett Zamir (http://brett-zamir.me) // - depends on: localeconv // - depends on: str_replace // * example 1: strtonumber('1,123.564', '.', ','); // * returns 1: 1123.564 // * example 2: strtonumber('1,123', '.', ',') // * returns 2: 1123 if(typeof dec_point === 'undefined' || typeof thousands_sep === 'undefined') { var locale = this.localeconv(); if(typeof dec_point === 'undefined') { dec_point = locale['decimal_point']; } if(typeof thousands_sep === 'undefined') { thousands_sep = locale['thousands_sep']; } } return parseFloat(this.str_replace(dec_point, '.', this.str_replace(thousands_sep, '', str))); } </code></pre> I was able to avoid the extra code used in the original (at the end) since JavaScript doesn't represent integers differently from floats as variable types. Be sure to add the dependencies and also note that localeconv() also has its own dependency of setlocale() which you also will need, at least if you may rely on defaults. But consider that the default locale set up by setlocale() was based on the default locale I obtained in my own testing which did not include a thousands separator (as one might expect the default to be ',' as English). So, unless you adapt the locale(s) inside setlocale(), you have to specify all of the arguments for strtonumber. Hope that helps... <hr /> <strong> Ronnie </strong> on 2010-01-07 21:34:44 <br /> I like it, but now I have to calculation on it, so I need to format the number back to a numeric format for example from 1,000,235.22 to 1000235.22 <hr /> <strong> Robert </strong> on 2010-01-02 15:31:18 <br /> thank you! You just made my life 20 times easier. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-25 13:37:16 <br /> @ Josh: as you can see this function has been revised 2 times. That means a complete rewrite. I guess what you could blame us for is regression. More specifically: not having a test case for the negative numbers. But hey, there are over 400 functions we have to maintain and we do all of this for free so please take it or by all means - leave it. @ Brett Zamir: I chose not to have an incremental version for each function because I felt that could easily lead to inconsistency. Instead it's the shortest form of the modification date that still makes sense. e.g.: 906.1806 would translate to 20090618 06:00:00 <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2009-10-11 07:26:58 <br /> @Josh: Although I don't know what Kevin's system of numbering is about exactly, I'm pretty darn sure it wasn't 900 revisions! Please keep in mind that these functions have been posted here by different people, often with submissions which started as mere approximations of the PHP function and over time have had a few bugs fixed, some more PHP behaviors added, etc. Not all of us have the time or experience to review each function in full detail, compare it with PHP behavior and its idiosyncrasies, etc. (and functions earlier on were accepted with more loose standards than now). A lot of functions were added in good faith. We're volunteers just like you are in submitting the patch, only Kevin has done the added work of making a website, a compiler, etc., just in the hopes of it being useful. So, take it or leave it for what it's worth. We'd all love to see a comprehensive review and have the functions put through all of the test cases as used in the PHP source code, but we're not there yet. I think maybe the Duct Tape Programmer philosophy applies to our project: http://www.joelonsoftware.com/items/2009/09/23.html :) @Andras, Thanks very much for the function. Before Kevin or I get a chance to look at it, could you be sure that it follows the PHP behavior at least as well as the one we have now--passing test cases, etc. and addressing Josh's issue? <hr /> <strong> Josh </strong> on 2009-10-10 23:47:44 <br /> Thank you for the function, however it boggles my mind that you can go through 906 revisions and still can't get negative numbers right (at least in Firefox 3.5.3 or IE 8). Negative numbers are numbers that are less than 0, and yes, they do come into play occasionally in this world. I added a couple changes at the very beginning and end of the function to account for the problem: <pre><code> //beginning var prefix = ''; if(number &lt; 0) prefix = '-'; //end return prefix + s; <hr /> <strong> Andras </strong> on 2009-10-10 20:24:25 <br /> This is shorter :) Feel free to change var names. <pre><code> function number_format(amount, nDec, sDec, sTho){ if (!sDec) sDec = ','; if (!sTho) sTho = '.'; amount = (amount * 1).toFixed(nDec); if (isNaN(amount)) return NaN; amountExp = (amount + '').split('.'); amount = amountExp[0]; var rgx = /(\d+)(\d{3})/; while (rgx.test(amount)) { amount = amount.replace(rgx,'$1' + sTho + '$2'); }; if (nDec &gt; 0){ amount += sDec + amountExp[1]; }; return amount; }; </code></pre> <hr /> <strong> Jay Klehr </strong> on 2009-06-19 17:40:55 <br /> Thanks Brett, looks good now. <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-06-19 00:07:40 <br /> Ok, great, thanks Amir. Fixed and tested (Mozilla). (Also allowed precision 1 to work in all cases.) <hr /> <strong> <a href="http://www.residence-mixte.com/" rel="nofollow">Amir Habibi</a> </strong> on 2009-06-18 21:59:55 <br /> To continu with Jay's comment, if you try : <pre><code> number_format('1.2', 2); </code></pre> You'll still get '1.2' instead of '1.20' <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-06-18 13:59:40 <br /> @ Brett Zamir: Thanks for fixing Brett!! <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-06-18 06:42:58 <br /> Thanks for the report, Jay. Should be fixed now. <hr /> <strong> Jay Klehr </strong> on 2009-06-11 17:45:25 <br /> When using number_format to format numbers used for currency, I noticed that if the number has a single decimal followed by a zero initially, and I apply number_format to it (with 2 decimal precision) I don't get 2 decimals. It strips the trailing zero. <pre><code> number_format('1.20', 2); // returns 1.2, should return 1.20 </code></pre> Using the above version as of this writing (906.111) <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2009-06-01 11:19:21 <br /> @Kheang Hok Chin, thanks for the report and @davook, thanks for the workaround! I've incorporated the workaround (and added some more test cases), and also fixed a problem when the final value was an absolute number and was thus not getting zeroes added. (FYI, the referenced discussion thread was missing a digit at the end: http://forums.mozillazine.org/viewtopic.php?f=9&amp;t=999945 ) <hr /> <strong> davook </strong> on 2009-05-25 13:28:42 <br /> Related to the last comment, i've the same problem. It seems that &quot;toFixed()&quot; is the problem: http://forums.mozillazine.org/viewtopic.php?f=9&amp;t=99994 As a temporary, bad and ugly solution i'm using an auxiliar function replacing toFixed(). Example: <pre><code> // Original code n.toFixed(prec); // New code toFixedFix(n, prec); </code></pre> The toFixedFix function would be like this: <pre><code> function toFixedFix(n,prec) { var k = Math.pow(10,prec); return (Math.round(n*k)/k).toString(); } </code></pre> Any idea how improve it? <hr /> <strong> <a href="www.distantia.ca" rel="nofollow">Kheang Hok Chin</a> </strong> on 2009-05-22 16:32:30 <br /> Hi, I have a question in regard to this comment: - bugfix by: Rival note 1: For 1000.55 result with precision 1 in FF/Opera is 1,000.5, but in IE is 1,000.6 Has this been fixed or does this mean that if a user using Firefox, Opera, Safari will always get the result 1000.5 when formatting the number 1000.55 to precision 1? While in IE it returns 1000.6 which is correct as number_format in PHP works the same way. Is there a fix for this? <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-02-15 15:23:33 <br /> @ Rival: Hey there, excellent work Rival! I've also added 2 test cases that cover your findings so we can't make that mistake again. Thanks! <hr /> <strong> Rival </strong> on 2009-02-12 10:31:11 <br /> cleanup <pre><code> .... var abs = Math.abs(s); .... </code></pre> <hr /> <strong> Rival </strong> on 2009-02-12 10:15:34 <br /> - show thousand separator for number=1000 - decimal point for numbers lower than 1000 is undefined note: for 1000.55 result with precision 1 in FF/Opera is 1,000.5, but in IE is 1,000.6 <pre><code> function number_format(number, decimals, dec_point, thousands_sep) { var n = number, prec = decimals; n = !isFinite(+n) ? 0 : +n; prec = !isFinite(+prec) ? 0 : Math.abs(prec); var sep = (typeof thousands_sep == &amp;quot;undefined&amp;quot;) ? ',' : thousands_sep; var dec = (typeof dec_point == &amp;quot;undefined&amp;quot;) ? '.' : dec_point; var s = (prec &amp;gt; 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); //fix for IE parseFloat(0.55).toFixed(0) = 0; var abs = Math.abs(n).toFixed(prec); var _, i; if (abs &amp;gt;= 1000) { _ = abs.split(/\D/); i = _[0].length % 3 || 3; _[0] = s.slice(0,i + (n &amp;lt; 0)) + _[0].slice(i).replace(/(\d{3})/g, sep+'$1'); s = _.join(dec); } else { s = s.replace('.', dec); } return s; } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-02-02 23:40:32 <br /> @ Diogo Resende: Confirmed. I've processed your fix. Thanks a lot! <hr /> <strong> <a href="n/a" rel="nofollow">Simon</a> </strong> on 2009-02-02 16:57:38 <br /> This does not work as is on IE6 and IE7, very simple to fix. search is not defined and ie throws an error. so just add var search; to the top of the script. <hr /> <strong> <a href="http://www.thinkdigital.pt" rel="nofollow">Diogo Resende</a> </strong> on 2009-02-02 16:55:53 <br /> It's not correct for numbers below 1000. It needs an else statement like this: <pre><code> if (abs &amp;gt; 1000) { ... } else { s = abs.replace('.', ','); } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-01-16 22:49:57 <br /> @ Luke: After I changed: <pre><code> prec = !isFinite(+prec) ? 2 : Math.abs(prec); </code></pre> To <pre><code> prec = !isFinite(+prec) ? 0 : Math.abs(prec); </code></pre> ..All test cases from the php manual worked perfectly. And since your code is a bit easier on the eyes as well, I'll include it in our project. Thanks a lot! <hr /> <strong> <a href="http://lucassmith.name" rel="nofollow">Luke</a> </strong> on 2009-01-16 09:31:46 <br /> I attacked the same problem recently and came up with a similar solution http://gist.github.com/47871 more lines, but fewer characters, and compresses (via YUI Compressor) to about 83% of the version above. Take a look; there may be improvements to be made. <hr /> <strong> <a href="http://memiux.com" rel="nofollow">Memiux</a> </strong> on 2009-01-15 19:45:18 <br /> Thanks, It's so beautiful and tiny :) <hr /> <strong> <a href="http://www.connectcase.nl" rel="nofollow">Cees</a> </strong> on 2009-01-14 09:21:56 <br /> This was so what I was looking for!!! Thanks, guys!!! <hr /> <strong> Mariusz </strong> on 2008-11-04 13:09:18 <br /> I take it back, got the latest version and all looks good. Thanks to those involved with the fixes! <hr /> <strong> Mariusz </strong> on 2008-11-04 13:05:26 <br /> This method does not seem to work correctly with negative numbers, for example (to 2 decimal places): -16038 returns: -160,38.00 -820 returns -,820.00 <hr /> <strong> <a href="http://www.getasupplier.com" rel="nofollow">Chad Smith</a> </strong> on 2008-08-28 17:00:54 <br /> Ignore my previous comment, I'm an idiot. I was checking to see if it was larger than 1000 before it ran through the function. Duh, sorry. Love the script! Chad <hr /> <strong> <a href="http://www.getasupplier.com" rel="nofollow">Chad Smith</a> </strong> on 2008-08-28 16:54:37 <br /> Anyone else notice that if you just put in 1000 it doesn't format the number to 1,000? But if you put in 1001 it formats correctly to 1,001. Any idea what could make that happen? <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-06-08 16:07:35 <br /> @ Howard Yeend: Thanks for noticing. This function was revised, and that reimported a previously fixed bug. It's probably because in some countries (including my own), the dec_point and thou_sep are indeed the other way around compared to the english standard. Anyway. fixed again :) <hr /> <strong> Howard Yeend </strong> on 2008-06-06 11:26:54 <br /> just a comment; aren't the default values for dec_point and thou_sep the wrong way around? by default it formats 12345.67 as 12.345,67 shouldn't it be 12,345.67 ? <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-04-13 12:44:13 <br /> @ Jonas Raoni: Thanks I've updated the function. I have one remark though. We generally compress code with packer &amp;amp; jsmin to obtain bandwidth &amp;amp; speed advantages, while maintaining readability so developers can more easily understand and update each others code. <hr /> <strong> Jonas Raoni </strong> on 2008-04-12 20:10:46 <br /> I corrected all of the bugs that I found: - Negative numbers; - Starting with zero; - Rouding (when cutting the decimal places of &amp;quot;0.9&amp;quot; it should be 1 not 0). And here is the last version of the code: Number.prototype.formatMoney = function(c, d, t){ var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? &amp;quot;,&amp;quot; : d, t = t == undefined ? &amp;quot;.&amp;quot; : t, s = n &amp;lt; 0 ? &amp;quot;-&amp;quot; : &amp;quot;&amp;quot;, i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + &amp;quot;&amp;quot;, j = (j = i.length) &amp;gt; 3 ? j % 3 : 0; return s + (j ? i.substr(0, j) + t : &amp;quot;&amp;quot;) + i.substr(j).replace(/(\d{3})(?=\d)/g, &amp;quot;$1&amp;quot; + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : &amp;quot;&amp;quot;); }; <hr /> <strong> Howard Yeend </strong> on 2008-04-09 18:11:44 <br /> yay for Allan Jensen. I just noticed the same bug he fixed. cheers dude. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-03-29 10:39:31 <br /> @ Allan Jensen: Wow thanks for fixing it! <hr /> <strong> Allan Jensen </strong> on 2008-03-28 23:19:53 <br /> Fixed the problem with negative numbers myself. Function needs to look like this: <pre><code> function number_format( number, decimals, dec_point, thousands_sep ) { // http://kevin.vanzonneveld.net // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfix by: Michael White (http://crestidg.com) // + bugfix by: Benjamin Lupton // + bugfix by: Allan Jensen (http://www.winternet.no) (fixed formatting negative numbers) // * example 1: number_format(1234.5678, 2, '.', ''); // * returns 1: 1234.57 var i, j, kw, kd, km; var neg = &amp;quot;&amp;quot;; // Input sanitation &amp;amp; defaults if ( isNaN(decimals = Math.abs(decimals)) ) { decimals = 2; } if (dec_point == undefined) { dec_point = &amp;quot;.&amp;quot;; } if (thousands_sep == undefined) { thousands_sep = &amp;quot;,&amp;quot;; } i = parseInt(number = (+number || 0).toFixed(decimals)) + &amp;quot;&amp;quot;; if (i.substr(0,1) == &amp;quot;-&amp;quot;) { number = Math.abs(number); neg = &amp;quot;-&amp;quot;; i = i.substr(1); } if ((j = i.length) &amp;gt; 3 ) { j = j % 3; } else { j = 0; } km = (j ? i.substr(0, j) + thousands_sep : &amp;quot;&amp;quot;); kw = i.substr(j).replace(/(\d{3})(?=\d)/g, &amp;quot;$1&amp;quot; + thousands_sep); //kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : &amp;quot;&amp;quot;); kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : &amp;quot;&amp;quot;); return neg + km + kw + kd; } </code></pre> <hr /> <strong> Allan Jensen </strong> on 2008-03-28 15:57:35 <br /> The function has a problem with negative numbers. This: number_format(-258.75, 2, &amp;quot;,&amp;quot;, &amp;quot;.&amp;quot;) incorrectly gives: -,258,75 Please help me fix this. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-03-20 15:05:56 <br /> @ Benjamin Lupton: Thank you! <hr /> <strong> Benjamin &quot;balupton&quot; Lupton </strong> on 2008-03-20 05:21:49 <br /> dec_point and thousands_sep defaults should be the other way round <hr /> <strong> Michael White </strong> on 2008-03-01 17:26:33 <br /> No problem. I love having these familiar PHP functions available. I have a sort of proposal for you, maybe you could contact me via email (michael -a-t- crestidg.com) so I can get you some more details. It probably wouldn't take too much time for us to do and it would benefit those who want to use the entire PHP.js package along side other JavaScript quite a lot. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-03-01 15:39:39 <br /> @ Michael White: It sure did, thanks for taking the time to supply better code. I've updated the function! <hr /> <strong> Michael White </strong> on 2008-03-01 07:33:49 <br /> There is a bug in Safari - at least in Safari 3.0.4 that causes the combination of Math.abs(0).toFixed(x) where x seems to be any positive integer to provide erroneous output. The sample code below contains problematic code at the top and a sample solution line at the end. The new code for the number_format() function actually is in a separate code block below this one. <pre><code> var price = 0.00; alert(Math.abs(price)); // Alerts: 0 - correct. // Alerts: 0.00 in most browsers. // In Safari 3.0.4 you get: 0.-0 alert(Math.abs(price).toFixed(2)); // However - If you skip the use of Math.abs() on a 0 value then the toFixed() function works correctly. var new_price = 0; alert(new_price.toFixed(2)); // Temp-fix ( I submitted this bug to Apple) var num = 0; var result = Math.abs(0).toFixed(1); alert(result); result = result.replace(/-/, 0); alert(result); </code></pre> Since this problem only occurs when working on a value of 0 (zero) to begin with we know that the hyphen character should be a 0 so we can simply replace it with a 0 if it is found. <pre><code> // The easiest way to fix this is to simply modify the line that reads: kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : &amp;quot;&amp;quot;); // with the following line of code: kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).replace(/-/, 0).slice(2) : &amp;quot;&amp;quot;); // The only difference is the addition of .replace(/-/, 0) just before the slice() method is used. // This tiny extra amount of processing should not be significant unless number_format() is run in very tight loops - and even then it can only be considered extra in browsers like IE, Opera, and Firefox since this appears to be a Safari only bug. Checking the browser seemed to be overkill in this instance. </code></pre> I hope it helps! Michael White - 2008 michael -a-t- crestidg.com http://crestidg.com/ <hr />