File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Setting currency problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Setting currency problem." Watch "Setting currency problem." New topic
Author

Setting currency problem.

Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Hi. I am a newbie to javascript so please excuse me if my question is obvious, but I just can't seem to solve it. I am working through the book 'Head First JavaScipt' by M Morrison and was working on a scipt called Duncan's Just-In-Time Donuts. The currency was set to dollars and I wanted to change it to pounds sterling. This is the script:



I have changed the $ signs to � (the section of Code is shown in bold), but when I run the script the � sign is preceded by a special character like so:-
��
my question is how do I eliminate the unwanted � sign?

I am using a Intel Mac pro using Leopard O/S and my browsers are Safari (default) and FireFox. I also have Opera but have not as yet run this script on it.

I hope you can help me with this as I live in the UK and will need to be able to use the � for currency in the majority of my scripts.

Regards denis
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Sorry ignore the comment re script in bold as it's not been shown in bold, It's this bit:

document.getElementById("subtotal").value = "�" + subTotal.toFixed(2);
document.getElementById("tax").value = "�" + tax.toFixed(2);
document.getElementById("total").value = "�" + total.toFixed(2);
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
The Pound sign is not part of the lower 128 ASCII characters, so the encoding of the page becomes an issue. Which one are you using? Something like

in the page header should work fine, assuming that's also the encoding in which you have saved the HTML/JS file on the server.


Ping & DNS - my free Android networking tools app
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Hi, Thanks for your reply. The DOCTYPE, meta and link are as below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/DTD/xhtml1/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Duncan's Just-In-Time Donuts</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" type="text/css" href="donuts.css" />

However I'm still producing the special character � before the � sign in my file!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
Looks good. And wherever the pound sign symbol is coming from, it is in fact ISO-8859-1 encoded?
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
So you are saying that my encoding of the page and my javascript are good, so why am I still displaying the unwanted � character?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
No, that's not what I'm saying. I'm saying that the way the encoding is specified looks good; what I'm asking is whether the encoding is good, too. Just saying that something is ISO-8859-1 encoded doesn't make it so. One way to check that would be to open whatever file contains the pound sign, and ensure that it is encoded by a single A0 character (which is hex for pound sign in ISO-8859-1).
Galen Palmer
Greenhorn

Joined: Aug 09, 2007
Posts: 29
I believe that you can replace the pound sign in the quotes to either &pound; or &#163;

[edit by Eric Pascarello - escaped the two amps so it would not show up as the character.]
[ April 01, 2008: Message edited by: Eric Pascarello ]
Galen Palmer
Greenhorn

Joined: Aug 09, 2007
Posts: 29
Sorry, should have escaped the entities:

Use: &pound; or &#163; instead of the pound character and it will be sure to work.
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Hello Ulf, Thanks for your replies, but I'm still not clear as to what I need to do! the code I quoted is the only file containing the � sign (which I put in in place of $ sign) i.e. from this:

document.getElementById("subtotal").value = "$" + subTotal.toFixed(2);
document.getElementById("tax").value = "$" + tax.toFixed(2);
document.getElementById("total").value = "$" + total.toFixed(2);

to this:

document.getElementById("subtotal").value = "�" + subTotal.toFixed(2);
document.getElementById("tax").value = "�" + tax.toFixed(2);
document.getElementById("total").value = "�" + total.toFixed(2);


so should I be be putting the A0 Hexadecimal number somewhere in this file and if so where?

The body of the file is below:

&it body &it
&it div id="frame" &it
&it div class="heading" &it Duncan's Just-In-Time Donuts &it/div &it
&it div class="subheading" &it All donuts 50 pence each, cake or glazed! &it /div &it
&it div id="left" &it
&it img src="donuttime.png" alt="Just-In-Time Donuts" /& it
& it/div &it
&it form name="orderform" action="donuts.php" method="POST" &it
<div class="field" &it
Name: & it input type="text" id="name" name="name" value="" / &it
&it /div &it
&it div class="field" &it
# of cake donuts: &it input type="text" id="cakedonuts" name="cakedonuts" value=""
onchange="updateOrder();" / &it
&it /div &it
&it div class="field" &it
# of glazed donuts: &it input type="text" id="glazeddonuts" name="glazeddonuts" value=""
onchange="updateOrder();" / &it
&it /div &it
&it div class="field" &it
Minutes 'til pickup: &it input type="text" id="pickupminutes" name="pickupminutes" value="" / &it
&it /div &it
&it div class="field" &it
Subtotal: &it input type="text" id="subtotal" name="subtotal" value="" readonly="readonly" / &it
&it /div &it
&it div class="field" &it
Tax: &it input type="text" id="tax" name="tax" value="" readonly="readonly" / &it
&it /div &it
&it div class="field" &it
Total: &it input type="text" id="total" name="total" value="" readonly="readonly" / &it
&it /div &it
&it div class="field" &it
&it input type="button" value="Place Order" onclik="placeOrder(this.form);" / &it
&it /div &it
&it /form &it
&it /div &it
&it /body &it
&it /html &it



The body refers to a PHP file which I have included below, though I'm not experienced enough to know if this would have any effect on the problem (I don't know PHP as yet)


&it html &it
&it body &it
&it ?php
$num = rand(1000, 10000);
$name = $_POST['name'];
$minutes = $_POST['pickupminutes'];
$total = $_POST['total'];

echo "&it h1 &it Duncan's Just-In-Time Donuts &it /h1 &it";
echo "&it h2 &it Order Confirmation &it /h2 &it";
echo "&it strong &it Order #" . $num . "&it /strong&it &it br / &it";
echo "Customer name: " . $name . "&it br / &it";
echo "Ready in: " . $minutes . " minutes &it br / &it";
echo "Total: &it em &it" . $total . "&it /em &it";
? &it
&it /body &it
&it /html &it


I'm apologise for not explaining this well but I am really new to javascript and find some of the terms confusing at this point.
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Hello Galen. Thank you for your post. If you not already realised I know next to nothing about JS yet so please forgive me if the answer to my question seems obvious to you! I looked at your post and I presumed that I should insert £ or £ in my code like so:

document.getElementById("subtotal").value = "£" + subTotal.toFixed(2);
document.getElementById("tax").value = "£" + tax.toFixed(2);
document.getElementById("total").value = "£" + total.toFixed(2);

However when I tried it, it didn't work. Can you see if i have gone wrong please?
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Hello Galen. Sorry about the above post, I meant to say that as I am still learning I was still unclear how to put the £ or £ into my script. I have this script:

document.getElementById("subtotal").value = "�" + subTotal.toFixed(2);
document.getElementById("tax").value = "�" + tax.toFixed(2);
document.getElementById("total").value = "�" + total.toFixed(2);

So I presumed you meant I should substitute either £ or £ for the � like so:

document.getElementById("subtotal").value = "£" + subTotal.toFixed(2);
document.getElementById("tax").value = "£" + tax.toFixed(2);
document.getElementById("total").value = "£" + total.toFixed(2);

However when I tried it, it didn't work. Can you please point out where I am going wrong?

sorry I'm such a dumbo,

Regards Denis
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Hello Galen. Sorry about the above post, I meant to say that as I am still learning I was still unclear how to put the £ or £ into my script. I have this script:

document.getElementById("subtotal").value = "�" + subTotal.toFixed(2);
document.getElementById("tax").value = "�" + tax.toFixed(2);
document.getElementById("total").value = "�" + total.toFixed(2);

So I presumed you meant I should substitute either & pound or & # 163 for the � sign like so:

document.getElementById("subtotal").value = "& # 163" + subTotal.toFixed(2);
document.getElementById("tax").value = "& # 163" + tax.toFixed(2);
document.getElementById("total").value = "& # 163" + total.toFixed(2);

However when I tried it, it didn't work. Can you please point out where I am going wrong?

sorry I'm such a dumbo,

Regards Denis
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Hello Galen. Sorry about the above post, I meant to say that as I am still learning I was still unclear how to put the £ or £ into my script. I have this script:

document.getElementById("subtotal").value = "�" + subTotal.toFixed(2);
document.getElementById("tax").value = "�" + tax.toFixed(2);
document.getElementById("total").value = "�" + total.toFixed(2);

So I presumed you meant I should substitute either & pound or & # 163 for the � sign like so:

document.getElementById("subtotal").value = "& # 163" + subTotal.toFixed(2);
document.getElementById("tax").value = "& # 163" + tax.toFixed(2);
document.getElementById("total").value = "& # 163" + total.toFixed(2);

However when I tried it, it didn't work. Can you please point out where I am going wrong?

sorry I'm such a dumbo,

Regards Denis
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Hello Galen. Sorry about the above post, I meant to say that as I am still learning I was still unclear how to put the £ or £ into my script. I have this script:

document.getElementById("subtotal").value = "�" + subTotal.toFixed(2);
document.getElementById("tax").value = "�" + tax.toFixed(2);
document.getElementById("total").value = "�" + total.toFixed(2);

So I presumed you meant I should substitute either & pound or & # 163 for the � sign like so:

document.getElementById("subtotal").value = "& # 163" + subTotal.toFixed(2);
document.getElementById("tax").value = "& # 163" + tax.toFixed(2);
document.getElementById("total").value = "& # 163" + total.toFixed(2);

However when I tried it, it didn't work. the result

was to show the & # 163 plus the price

Can you please point out where I am going wrong?

sorry I'm such a dumbo,

Regards Denis
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
The semi-colon is missing behind the 163. You can use either &#163; or &pound; (which is a bit easier to make sense of).
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Hi Ulf, I followed your guide and added the semicolon like so "& pound;" without the space between & and pound, and when i ran my file it returned £1.09 instead of �1.09
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
sorry it returned & pound;1.00
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41867
    
  63
That's odd. But the &#163; thing should work regardless.
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
It works if I use it in the post on this forum thats why I ended up repeating my earlier post to Galen?

I'm completely foxed?
Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Hi guy, You will be pleased to know I have found the solution, It's unicode. I changed my script to read like so:

document.getElementById("subtotal").value = "\u00A3" + subTotal.toFixed(2);
document.getElementById("tax").value = "\u00A3" + tax.toFixed(2);
document.getElementById("total").value = "\u00A3" + total.toFixed(2);

The unicode for the � is in Bold. I ran this and it worked perfectly. I posted the solution for other users benefit.

Thank you for your efforts and the time you have spent assisting me with this problem. Currency appears to be a difficult area to find solutions for. Once again your help has been much appreciated.

Regards Denis, javascript:%20x()


Denis Williams
Greenhorn

Joined: Dec 30, 2007
Posts: 19
Incidentally I tried the & pound sign in the script contained in the body of a site I am building and it worked fine. where as the unicode would not work in this case. So it's horses for courses, at least when it comes to the UK currency sign.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting currency problem.