"Andie" <andie (AT) mansuntv (DOT) co.uk> wrote
Quote:
Hello All,
I have a form which has various options in it, with a total cost field,
the total cost will change when a certain option is chosen, I seem to be
having a problem getting the form to display the value correctly.
For example, I can get the form displaying the data for 139.99, but if I
have a whole amount like 4.00 it only displays 4. |
I made function for this purpouse in my own application, that converts
floats human readable numbers. 0 is "0.00", 4 is "4.00" 1023456.2 is
"1,023,456.20", so it also adds commas to separate thousands, and presents
two decimals at all times. The thing is, you must also be prepared at server
to recieve the number in the thousands-separated format. If you don't like
this, just change the following lines:
var buff = "";
for(j=-1, i=integerpart.length; i>=0; i--, j++){
if((j%3) == 0 && j>1)
buff = "," + buff;
buff = integerpart.charAt(i) + buff;
}
return buff+"."+decimalpart;
to simply
return integerpart+"."+decimalpart;
here's the function:
function floatToStr(aFloat){
if(!aFloat)
return "0.00";
integerpart = (aFloat<0) ? Math.ceil(aFloat) : Math.floor(aFloat);
decimalpart = Math.abs(Math.round((aFloat - integerpart)*100));
integerpart+= '';
if(decimalpart<10)
decimalpart="0"+decimalpart;
if(decimalpart==0)
decimalpart="00";
var buff = "";
for(j=-1, i=integerpart.length; i>=0; i--, j++){
if((j%3) == 0 && j>1)
buff = "," + buff;
buff = integerpart.charAt(i) + buff;
}
return buff+"."+decimalpart;
}
Hope this helps. It has not been optimized, but it has been in use for quite
some time now and I haven't gotten any complaints of, so it should work.
Pretty fucking ugly, but it works and that's good enough for me.
--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
eternal piste erection miuku gmail piste com