Scrote
11-07-2003, 11:22 AM
Hey,
Pretty new to JS (used it at Uni for about two weeks...about two years ago), and looking for some advice, if anyone is willing.
I have a script that produces and populates a table with some calculated shipping values. Everything seems to *work* ok, but just cant get it to *look* right. I can't get the ("currency") values in my table line up nice and neat.
eg. $4.9 instead of $4.90 so,
$5.47
$4.9
$0.1
$13.67
starts to look pretty crappy when aligned in a table.
I tried mucking around with this code for awhile (to be used in conjunction with my code):
<script>
/*Parse number to currency format:
By JavaScript Kit (www.javascriptkit.com)
Over 200+ free scripts here!
*/
//Remove the $ sign if you wish the parse number to NOT include it
var prefix="$"
var wd
function parseelement(thisone){
if (thisone.value.charAt(0)=="$")
return
wd="w"
var tempnum=thisone.value
for (i=0;i<tempnum.length;i++){
if (tempnum.charAt(i)=="."){
wd="d"
break
}
}
if (wd=="w")
thisone.value=prefix+tempnum+".00"
else{
if (tempnum.charAt(tempnum.length-2)=="."){
thisone.value=prefix+tempnum+"0"
}
else{
tempnum=Math.round(tempnum*100)/100
thisone.value=prefix+tempnum
}
}
}
</script>
but, I couldn't get the results I was after.
I think it's because I dont know what Im doing.
Below is a copy of the (working) code that I have thusfar. I get the feeling that I have more variables and algebra than I need, I'm just happy it works atm.
Also, I have copied some code (the roundit() function) form javascriptkit.com, should I acknowldege them in the code? Afterall, i would hate to break some kinda crazy "coder" ethics .
If you run the code in a browser, then you will quickly see what I mean about the alignments.
I figured that I could acheive the desired effect using html and fixed width tables with some css, but getting the JS right just seems so much more professional :).
Even if somebody could just point me at some useful resources, I would be most grateful.
Thanks for your time and help,
-Scrote
<html>
<body>
<script language="JavaScript1.1" type="text/javascript">
<!--
//Variables - known shipping values
var shi = 1.20;
var env = 0.60;
var top = 0.20;
var sle = 0.05;
var lis = 0.47;
//Functions
//Rounding function that puts me *close* to a currency format
function roundit(Num, Places) {
if (Places > 0) {
if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
var Rounder = Math.pow(10, Places);
var num = Math.round(Num * Rounder) / Rounder;
return Math.round(Num * Rounder) / Rounder;
}
else return Num;
}
else return Math.round(Num);
}
//Creates and populates my table
function shipping () {
var i = 0;
var result = 0;
var status = true;
document.writeln("<table cellpadding=0 cellspacing=0 align=center width=450 align=center><tr><th># Singles Won</th><th>Total Shipping Cost ($US)</th><th>Cost/Card</th></tr>");
while(status){
count = ++i;
var r = Math.round(count/2);
var result = Math.round(r);
var rtop = result * top;
var rsle = result * sle;
var rlis = count * lis;
var total = shi + env + rtop + rsle + rlis;
var perCard = (shi + env + rtop + rsle + rlis)/count;
var roundPerCard = roundit(perCard, 2);
var roundTotal = roundit(total, 2);
document.writeln("<tr align=center><td>" + count + "</td><td>$ " + roundTotal +"</td><td>$ " + roundPerCard + "</td></tr>")
if(i == 200){
status = false;
}
}
document.writeln("</table>");
}
shipping();
// --> </script>
</body>
</html>
Pretty new to JS (used it at Uni for about two weeks...about two years ago), and looking for some advice, if anyone is willing.
I have a script that produces and populates a table with some calculated shipping values. Everything seems to *work* ok, but just cant get it to *look* right. I can't get the ("currency") values in my table line up nice and neat.
eg. $4.9 instead of $4.90 so,
$5.47
$4.9
$0.1
$13.67
starts to look pretty crappy when aligned in a table.
I tried mucking around with this code for awhile (to be used in conjunction with my code):
<script>
/*Parse number to currency format:
By JavaScript Kit (www.javascriptkit.com)
Over 200+ free scripts here!
*/
//Remove the $ sign if you wish the parse number to NOT include it
var prefix="$"
var wd
function parseelement(thisone){
if (thisone.value.charAt(0)=="$")
return
wd="w"
var tempnum=thisone.value
for (i=0;i<tempnum.length;i++){
if (tempnum.charAt(i)=="."){
wd="d"
break
}
}
if (wd=="w")
thisone.value=prefix+tempnum+".00"
else{
if (tempnum.charAt(tempnum.length-2)=="."){
thisone.value=prefix+tempnum+"0"
}
else{
tempnum=Math.round(tempnum*100)/100
thisone.value=prefix+tempnum
}
}
}
</script>
but, I couldn't get the results I was after.
I think it's because I dont know what Im doing.
Below is a copy of the (working) code that I have thusfar. I get the feeling that I have more variables and algebra than I need, I'm just happy it works atm.
Also, I have copied some code (the roundit() function) form javascriptkit.com, should I acknowldege them in the code? Afterall, i would hate to break some kinda crazy "coder" ethics .
If you run the code in a browser, then you will quickly see what I mean about the alignments.
I figured that I could acheive the desired effect using html and fixed width tables with some css, but getting the JS right just seems so much more professional :).
Even if somebody could just point me at some useful resources, I would be most grateful.
Thanks for your time and help,
-Scrote
<html>
<body>
<script language="JavaScript1.1" type="text/javascript">
<!--
//Variables - known shipping values
var shi = 1.20;
var env = 0.60;
var top = 0.20;
var sle = 0.05;
var lis = 0.47;
//Functions
//Rounding function that puts me *close* to a currency format
function roundit(Num, Places) {
if (Places > 0) {
if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
var Rounder = Math.pow(10, Places);
var num = Math.round(Num * Rounder) / Rounder;
return Math.round(Num * Rounder) / Rounder;
}
else return Num;
}
else return Math.round(Num);
}
//Creates and populates my table
function shipping () {
var i = 0;
var result = 0;
var status = true;
document.writeln("<table cellpadding=0 cellspacing=0 align=center width=450 align=center><tr><th># Singles Won</th><th>Total Shipping Cost ($US)</th><th>Cost/Card</th></tr>");
while(status){
count = ++i;
var r = Math.round(count/2);
var result = Math.round(r);
var rtop = result * top;
var rsle = result * sle;
var rlis = count * lis;
var total = shi + env + rtop + rsle + rlis;
var perCard = (shi + env + rtop + rsle + rlis)/count;
var roundPerCard = roundit(perCard, 2);
var roundTotal = roundit(total, 2);
document.writeln("<tr align=center><td>" + count + "</td><td>$ " + roundTotal +"</td><td>$ " + roundPerCard + "</td></tr>")
if(i == 200){
status = false;
}
}
document.writeln("</table>");
}
shipping();
// --> </script>
</body>
</html>