Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

Feedback: irt.org FAQ Knowledge Base Q8

Feedback on: irt.org FAQ Knowledge Base Q8

Sent by Lutz Kretzschmar on April 05, 2000 at 12:10:34: - feedback #1044

Worth:
Very worth reading

Comments:
Both functions fail for input with decimal places.
Here is my version:

function thousands(str)
{
var saveStr = "" + str;
var decipos=saveStr.indexOf('.');
var deciStr="";
if (decipos>=0)
{
deciStr+=saveStr.substring(decipos,100);
saveStr=saveStr.substring(0,decipos);
}
if (saveStr.length < 4) return str;
var revStr = reverseIt(saveStr);
var newStr = '';
for (var i=0;i {
if (i>0 && (i%3)==0) newStr += ',';
newStr += revStr.charAt(i);
}
newStr=reverseIt(newStr);

if (decipos>=0)
{
newStr+=deciStr;
}
return(newStr);

}



Sent by Richard Balfour on April 11, 2002 at 13:43:07: - feedback #3775

Worth:
Very worth reading

Comments:
I used Val Todorov's method but had to add a test for negative number, reverse sign of number then put sign in front of join as: var sign = "";
if (n<0){sign = "-";n = n*(-1)}
then return sign+arr.join();.

Thanks for the help.


Sent by Steve M. McRoberts on August 19, 2002 at 09:05:09: - feedback #4082

Worth:
Very worth reading

Comments:
It doesn't look like these can handle negative numbers. Here is a version based on Val Todorov's, which handles negative numbers as well as decimals.

<script language="JavaScript1.1"></script>
<form>
<input type="text" name="input">
<input type="text" name="output">
<input type="button"
onClick="this.form.output.value=number_comma_format(this.form.input.value)" VALUE="Number Format">
</form>




Sent by Bryan Price on Wednesday May 23, 2007 at 16:43:23 - feedback #4616

Worth:
Not worth reading

Length:

Technical:

Comments:
A slightly complicated version:

var n = 12345678.91011;

function commas(n){
var d = (''+n).split('.'), x=(d[0].length+2)%3;
d[0] = ('00'.slice(x)+d[0]).match(/\d{3}/g).join(',').slice(2-x);
return d.join('.');
}





Sent by Heron Anzures on Friday February 22, 2008 at 14:10:52 - feedback #5217

Worth:
Very worth reading

Length:
Just right

Technical:
Just right

Comments:
This solution also works with decimals and negatives.

<script type="text/javascript">

function format(number)
{
var text = new String(number);
var negative = text.charAt(0) == '-';
while(true)
{
var i = text.indexOf(",");
if(i == -1)
i = text.indexOf(".");
if(i == -1)
i = text.length;
if(i > 3 && !negative || i > 4)
text = text.substring(0,i-3)+","+text.substring(i-3);
else
return text;
}
}
document.write(format(-123456789) + "<br/>");
document.write(format(1233456.13) + "<br/>");

</script>




©2018 Martin Webb