HighDots Forums  

Can anyone help with this, I am stuck!

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss Can anyone help with this, I am stuck! in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Andie
 
Posts: n/a

Default Can anyone help with this, I am stuck! - 08-25-2005 , 01:18 AM






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.

Another problem I have is say I have an item for 9.99 with 3 color options
with costs of either 1.00,2.00 or 3.00

If I select the option one, the total displays correctly, ie 10.99, and if I
select option 2 it displays 11.99, but if I change my mind and want option 1
it displays the data like this 10.990000, any ideas.

This is the script I have.

<SCRIPT LANGUAGE="JavaScript">

function onLoad()
{

if (document.additem.OPTidOption3)
{
document.additem.OPTidOption3.selectedIndex=0;
}

changePrice(document.additem.total.value);
}


function changePrice(newPrice)
{
var newPriceVatN = newPrice ;
var newPriceVat = newPriceVatN.toString();
var priceArray = newPriceVat.split('.');
if (priceArray.length == 2)
{
if (priceArray[1].length == 1)
{
newPriceVat = priceArray[0] + '.' + priceArray[1] + '0';

}
else if(priceArray[1].length > 2)
{
var price2N = priceArray[1];
var price2 = new String(price2N);

var dig1 = new String(price2.substring(0,1));
var dig2 = new String(price2.substring(1,2));
var dig3 = new String(price2.substring(2,3));
var dig1and2 = new String(dig1 + dig2);
var dig1and2num = parsefloat(dig1and2);

var thirddigit = parsefloat(dig3);

if (thirddigit > 4)
{
dig1and2num++;
}
newPriceVat = parseFloat(priceArray[0] + '.' +
dig1and2num);

}
}
else
{
newPriceVat = priceArray[0];


}
}



function OPTidOption3FUNC(theForm)
{
var newVal =
parseFloat(OPTidOption3ARRAY[theForm.OPTidOption3.selectedIndex]);
if (newVal != theForm.OPTidOption3SEL.value)
{
theForm.total.value =
parseFloat(theForm.total.value) - parseFloat(theForm.OPTidOption3SEL.value);
theForm.total.value = newVal +
parseFloat(theForm.total.value);
theForm.OPTidOption3SEL.value = newVal;
changePrice(theForm.total.value);
}
}
OPTidOption3ARRAY = new Array();
OPTidOption3ARRAY[0] = 0.00;


OPTidOption3ARRAY[1] = 99.00

OPTidOption3ARRAY[2] = 499.00

OPTidOption3ARRAY[3] = 249.00

</Script>



Many thanks in advance

Andrew



Reply With Quote
  #2  
Old   
Kimmo Laine
 
Posts: n/a

Default Re: Can anyone help with this, I am stuck! - 08-25-2005 , 01:43 AM






"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




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.