473,796 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to display numbers as images - nuff easy

<html>
<head>
<title>JavaScri pt Replace</title>

<script language="JavaS cript1.1" type="text/javascript">
function phils_image_pri ce(input)
{
//declare variables
var output=""
var position=0
var chr=""
//loop
for (var position=0; position < input.length; position++)
{
chr=input.subst ring(position,p osition+1)
//replace
if (chr == '£') {output=output + '<img border="0"
src="img/pound.gif">'}
else if(chr == '0') {output=output + '<img border="0"
src="img/0.gif">'}
else if(chr == '1') {output=output + '<img border="0"
src="img/1.gif">'}
else if(chr == '2') {output=output + '<img border="0"
src="img/2.gif">'}
else if(chr == '3') {output=output + '<img border="0"
src="img/3.gif">'}
else if(chr == '4') {output=output + '<img border="0"
src="img/4.gif">'}
else if(chr == '5') {output=output + '<img border="0"
src="img/5.gif">'}
else if(chr == '6') {output=output + '<img border="0"
src="img/6.gif">'}
else if(chr == '7') {output=output + '<img border="0"
src="img/7.gif">'}
else if(chr == '8') {output=output + '<img border="0"
src="img/8.gif">'}
else if(chr == '9') {output=output + '<img border="0"
src="img/9.gif">'}
else if(chr == '.') {output=output + '<img border="0"
src="img/dot.gif">'}
else {output=output + chr}
}
return output;
}
</script>
</head>
<body>

<script language="JavaS cript1.1" type="text/javascript">
document.write( '£12345678.90')
document.write( '<br>')
document.write( phils_image_pri ce('£12345678.9 0'))
</script>

</body>
</html>

Oct 26 '05 #1
4 2504

go****@budget-edi.co.uk wrote:
<html>
<head>
<title>JavaScri pt Replace</title>

<script language="JavaS cript1.1" type="text/javascript">
There is no need for the language attribute.
function phils_image_pri ce(input)
{
//declare variables
var output=""
var position=0
var chr=""
//loop
for (var position=0; position < input.length; position++)
{
chr=input.subst ring(position,p osition+1)
//replace
if (chr == '£') {output=output + '<img border="0"
src="img/pound.gif">'}
else if(chr == '0') {output=output + '<img border="0"
src="img/0.gif">'}
else if(chr == '1') {output=output + '<img border="0"
src="img/1.gif">'}
else if(chr == '2') {output=output + '<img border="0"
src="img/2.gif">'}
else if(chr == '3') {output=output + '<img border="0"
src="img/3.gif">'}
else if(chr == '4') {output=output + '<img border="0"
src="img/4.gif">'}
else if(chr == '5') {output=output + '<img border="0"
src="img/5.gif">'}
else if(chr == '6') {output=output + '<img border="0"
src="img/6.gif">'}
else if(chr == '7') {output=output + '<img border="0"
src="img/7.gif">'}
else if(chr == '8') {output=output + '<img border="0"
src="img/8.gif">'}
else if(chr == '9') {output=output + '<img border="0"
src="img/9.gif">'}
else if(chr == '.') {output=output + '<img border="0"
src="img/dot.gif">'}
else {output=output + chr}
See all that if/else statements you have? This can be more efficient
through the use of the switch statement. Arguably, they will produce
the same results, however, in this case, the switch statement will
execute faster because there's no need to do multiple comparisons. For
example, if the next character was a '.', then it would have to do 12
comparisons, while a switch statement would do a single jump. }
return output;
}
</script>
</head>
<body>

<script language="JavaS cript1.1" type="text/javascript">
document.write( '£12345678.90')
document.write( '<br>')
document.write( phils_image_pri ce('£12345678.9 0'))
</script>

</body>
</html>


In the end, you are limited to doing document.write. But that may be
all you need. However, I would prefer to make it more dynamic and
create those number images on the fly.

Oct 26 '05 #2
go****@budget-edi.co.uk said the following on 10/26/2005 1:41 PM:
<html>
<head>
<title>JavaScri pt Replace</title>

<script language="JavaS cript1.1" type="text/javascript">


Unless you fully understand the ramifications of specifying a language
and version it is best left alone. Especially since the language
attribute is deprecated and the type attribute is preferred/required.

<snip code>

Looks like a good candidate for a switch statement.

Was there a question in there anywere?

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 26 '05 #3
go****@budget-edi.co.uk wrote:
<html>
<head>
For a Valid HTML document, on the top a DOCTYPE declaration missing
and where you read this text a character set declaration is missing.

<http://validator.w3.or g/>
<title>JavaScri pt Replace</title>

<script language="JavaS cript1.1" type="text/javascript">
You are using the deprecated `language' attribute without
purpose, which is why it is entirely redundant here.
function phils_image_pri ce(input)
function pointedEars_ima ge_price(s)
{
//declare variables
var output=""
This variable is unnecessary since `input' can be transformed
(i.e. substrings replaced) and the result of that returned instead.
var position=0
The declaration of `position' here may also be omitted, see below.
var chr=""
It is good practice to end JS statements with a semicolon,
just to avoid problems with automatic semicolon insertion.
//loop
for (var position=0; position < input.length; position++)
Much faster is

for (var position = input.length; position--;)

And if `position' was declared at the top, it was needlessly
redeclared here. Since it refers to the loop, I rather declare
it here than on the top.
{
chr=input.subst ring(position,p osition+1)
Have you never heard of String.prototyp e.charAt(...)?
//replace
if (chr == '£') {output=output + '<img border="0"
src="img/pound.gif">'}
else if(chr == '0') {output=output + '<img border="0"
src="img/0.gif">'}
else if(chr == '1') {output=output + '<img border="0"
src="img/1.gif">'}
else if(chr == '2') {output=output + '<img border="0"
src="img/2.gif">'}
else if(chr == '3') {output=output + '<img border="0"
src="img/3.gif">'}
else if(chr == '4') {output=output + '<img border="0"
src="img/4.gif">'}
else if(chr == '5') {output=output + '<img border="0"
src="img/5.gif">'}
else if(chr == '6') {output=output + '<img border="0"
src="img/6.gif">'}
else if(chr == '7') {output=output + '<img border="0"
src="img/7.gif">'}
else if(chr == '8') {output=output + '<img border="0"
src="img/8.gif">'}
else if(chr == '9') {output=output + '<img border="0"
src="img/9.gif">'}
else if(chr == '.') {output=output + '<img border="0"
src="img/dot.gif">'}
else {output=output + chr}
}
return output;
Or you *might* want to consider

return s.replace(
/(£)|([0-9])|(\.)/g,
function (str, p1, p2, p3, offset, s)
{
var filename;

if (p1)
{
filename = "pound";
}
else if (p2)
{
filename = p2;
}
else if (p3)
{
filename = "dot";
}

return ['<img src="', filename, '.gif" alt="',
(p1 || p2 || p3), '" border="0">'].join('');
});

instead.
}
</script>
</head>
<body>

<script language="JavaS cript1.1" type="text/javascript">
Again the unnecessary deprecated `language' attribute.
document.write( '£12345678.90')
document.write( '<br>')
document.write( phils_image_pri ce('£12345678.9 0'))
document.write(
['£12345678.90<b r>',
pointedEars_ima ge_price('£1234 5678.90')].join(''));
</script>

HTH & HAND

PointedEars
Oct 26 '05 #4
JRS: In article <11************ **********@g43g 2000cwa.googleg roups.com>
, dated Wed, 26 Oct 2005 11:04:46, seen in news:comp.lang. javascript,
web.dev <we********@gma il.com> posted :
for (var position=0; position < input.length; position++)
{
chr=input.subst ring(position,p osition+1)
//replace
if (chr == '£') {output=output + '<img border="0"
src="img/pound.gif">'}
else if(chr == '0') {output=output + '<img border="0"
src="img/0.gif">'}
else if(chr == '1') {output=output + '<img border="0"
src="img/1.gif">'}
else if(chr == '2') {output=output + '<img border="0"
src="img/2.gif">'}
else if(chr == '3') {output=output + '<img border="0"
src="img/3.gif">'}
else if(chr == '4') {output=output + '<img border="0"
src="img/4.gif">'}
else if(chr == '5') {output=output + '<img border="0"
src="img/5.gif">'}
else if(chr == '6') {output=output + '<img border="0"
src="img/6.gif">'}
else if(chr == '7') {output=output + '<img border="0"
src="img/7.gif">'}
else if(chr == '8') {output=output + '<img border="0"
src="img/8.gif">'}
else if(chr == '9') {output=output + '<img border="0"
src="img/9.gif">'}
else if(chr == '.') {output=output + '<img border="0"
src="img/dot.gif">'}
else {output=output + chr}


See all that if/else statements you have? This can be more efficient
through the use of the switch statement. Arguably, they will produce
the same results, however, in this case, the switch statement will
execute faster because there's no need to do multiple comparisons. For
example, if the next character was a '.', then it would have to do 12
comparisons, while a switch statement would do a single jump.


I think you are optimistic about how a switch statement works
internally. Unless the javascript engine does rather intelligent pre-
processing, the switch statement will still do the comparisons, though
it could do them faster.

More importantly, it's silly to use that approach for the digits. They
can be treated together; and, as they are presumably the most numerous
characters, they should be treated first.

Consider

XX = ""
if (chr>="0" && chr<="9") XX = chr
else if (chr=="£") XX = "pound"
else if (chr==".") XX = "dot" ;
if (XX) output += '<img border="0" src="img/' + XX + '.gif">'}
else output += chr ;

which can be reduced to

XX = chr>="0" && chr<="9" ? chr :
chr=="£" ? "pound" : chr=="." ? "dot" : ""
output += XX ? '<img border="0" src="img/' + XX + '.gif">' : chr ;

Alternatively :

AA = "0123456789 .£"
ZZ = [0,1,2,3,4,5,6,7 ,8,9,"dot","pou nd"]
XX = AA.indexOf(chr)
output += XX<0 ? chr : '<img border="0" src="img/' + ZZ[XX] + '.gif">' ;

which might extend better to other circumstances.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 27 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1226
by: Celine | last post by:
Hi everyone! I am building a website in asp.net using c# where I need to be able to upload 100 images to the server at a time. As I am new to asp.net, I was wondering if there was an easy way to do it. These images also need to be stored in a database(Sql server). Thanks for your help Celine
12
5231
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's RAND(). any ideas? I suppose I could use the current rancom number as the seed for the next random number. but would that really work?
12
2849
by: comp.lang.php | last post by:
index.php: // STUFF // STEP 1: imagecreatetruecolor ONLY IF GD 2.0+ SUPPORTED AND FOUND if ($this->isSuccessful && !$hasMogrified && $image && !$newImage && function_exists('imagecreatetruecolor') && preg_match('/2\.0/i', $this->gd_info_array)) { $newImage = @imagecreatetruecolor($configArray, $configArray);
18
2500
by: fishwick | last post by:
I haven't really done any css in quite a while, and am banging my head against the wall trying get the rudimentary layout together of a church website home page to display correctly - I don't want to continue work until I understand what I'm doing wrong. http://www.christchurchinfo.net The site displays nicely in Firefox/Mozilla and Safari, but the stained glass windows navigation portion is getting pushed down in IE, and the "news"...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.