newbie question about numbers

H

hakim

HI,

I am trying to write the following script:

<html>
<head><title>Test</title></head>


<script type="text/javascript">
var i = 10;

for(i = 0.1; i <= 5; i += 0.1) {
document.write("<div style=\"font-size:" + i + "em;\">"
+ i + "em = Hallo Welt!</div><br>");
}


</script>

<body>
</body>

</html>


But in the browser I see somwthing like that:

0.7999999999999999em = Hallo Welt!

I want it like 0.8 in this case. How I can arrange that with
javascript.

Thanks a lot...

Achim
 
M

Matthew Crouch

hakim said:
HI,

I am trying to write the following script:

<html>
<head><title>Test</title></head>


<script type="text/javascript">
var i = 10;

for(i = 0.1; i <= 5; i += 0.1) {
document.write("<div style=\"font-size:" + i + "em;\">"
+ i + "em = Hallo Welt!</div><br>");
}


</script>

<body>
</body>

</html>


But in the browser I see somwthing like that:

0.7999999999999999em = Hallo Welt!

I want it like 0.8 in this case. How I can arrange that with
javascript.

Thanks a lot...

Achim
http://www.javascriptkit.com/javatutors/round.shtml

not to be a dick, 'cause i'm a newbie but did you google "javascript
rounding numbers"... that's what i did.
 
R

Randy Webb

hakim said the following on 10/7/2005 12:34 PM:
HI,

I am trying to write the following script:

<html>
<head><title>Test</title></head>


<script type="text/javascript">
var i = 10;

Why do you initiate i = 10 here and then the next line you set it to 0.1?
for(i = 0.1; i <= 5; i += 0.1) {
document.write("<div style=\"font-size:" + i + "em;\">"
+ i + "em = Hallo Welt!</div><br>");
}

for (var i=1;i<=50; i++){
document.write('<div style="font-size:0.' +
</script>

<body>
</body>

</html>


But in the browser I see somwthing like that:
0.7999999999999999em = Hallo Welt!

I want it like 0.8 in this case. How I can arrange that with
javascript.

Stop counting in decimal, count in whole numbers.
 
D

David Wahler

Randy said:
Why do you initiate i = 10 here and then the next line you set it to 0.1?


for (var i=1;i<=50; i++){
document.write('<div style="font-size:0.' +
i + '"em;">0.' + i + 'em = Hallow Welt</div><br>');
} ....

Stop counting in decimal, count in whole numbers.

Er... just a thought, but maybe 0.10em != 1.0em?
 
R

Randy Webb

David Wahler said the following on 10/7/2005 5:51 PM:
Er... just a thought, but maybe 0.10em != 1.0em?

No it doesn't. But, the first code he posted counted from 0.1 to 5 in
increments of .1, that means he wants .1, .2, .3 ,.4 ,.5 and so on :)

But, to get from 1 to 50 EM, simply remove the two 0.'s in the
document.write statement:

for (var i=1;i<=50; i++){
document.write('<div style="font-size:' +
i + '"em;">' + i + 'em = Hallow Welt</div>');
}
 
M

Michael Winter

On 07/10/2005 22:45, Randy Webb wrote:

[snip]
for (var i=1;i<=50; i++){
document.write('<div style="font-size:0.' +
i + '"em;">0.' + i + 'em = Hallow Welt</div><br>');
^
Remove the first inner quote.

[snip]

Mike
 
D

Dr John Stockton

JRS: In article <kFx1f.1091$Tn5.553@trnddc08>, dated Fri, 7 Oct 2005
16:48:16, seen in Matthew Crouch <matthew.cro
(e-mail address removed)> posted :
http://www.javascriptkit.com/javatutors/round.shtml

not to be a dick, 'cause i'm a newbie but did you google "javascript
rounding numbers"... that's what i did.

Google, and the Web in general, is full of advice both good and bad.
The intelligent News user will read the newsgroup FAQ first.


Standard output-rounding apart, the OP has chosen a loop incrementing by
0.1, which cannot be represented exactly.

He had
var i = 10;

for(i = 0.1; i <= 5; i += 0.1) {

The '= 10' is pointless; and the loop would better be started by
for (j = 1; j <= 50; j++) { i = j/10

Note I've used /10 instead of *0.1 : both have rounding errors in the
arithmetic operation, but only the former one has an exact constant.

Always use exact arithmetic where practicable; it never does harm, and
may do good.
 
R

Robi

Randy Webb wrote in message news:[email protected]...
hakim said the following on 10/7/2005 12:34 PM: [...]
<script type="text/javascript">
var i = 10;

Why do you initiate i = 10 here and then the next line you set it to 0.1?

good point Randy, but
for(i = 0.1; i <= 5; i += 0.1) {
[...]

for (var i=1;i<=50; i++){
document.write('<div style="font-size:0.' + i +
^^^^^^^
this isn't quite what hakim wants ;-)
it would only get from 0.1 to 0.9 and then start with 0.10, 0.11 until 0.50
and not as hakim coded it from 0.1 to 5 in .1 increments
'"em;">0.' + i + 'em = Hallow Welt</div><br>');
}

document.write('<div style="font-size:' + i/10 + 'em;">' + i/10 + 'em = Hallo Welt</div><br>');

would be more like it
[...]
Stop counting in decimal, count in whole numbers.

absolutely correct, but after counting in whole numbers divide
it by 10 to get the desired value.
adding a '0.' in front of whole numbers doesn't quite work as decimals
that is, unless your decimals never turn into whole numbers ;-)
 
R

Randy Webb

Robi said the following on 10/7/2005 9:05 PM:
Randy Webb wrote in message
hakim said the following on 10/7/2005 12:34 PM:
[...]
<script type="text/javascript">
var i = 10;

Why do you initiate i = 10 here and then the next line you set it to 0.1?


good point Randy, but

for(i = 0.1; i <= 5; i += 0.1) {
[...]


for (var i=1;i<=50; i++){
document.write('<div style="font-size:0.' + i +

^^^^^^^
this isn't quite what hakim wants ;-)
it would only get from 0.1 to 0.9 and then start with 0.10, 0.11 until 0.50
and not as hakim coded it from 0.1 to 5 in .1 increments

YIKES!!!
'"em;">0.' + i + 'em = Hallow Welt</div><br>');
}


document.write('<div style="font-size:' + i/10 + 'em;">' + i/10 + 'em = Hallo Welt</div><br>');

would be more like it
[...]

Stop counting in decimal, count in whole numbers.


absolutely correct, but after counting in whole numbers divide
it by 10 to get the desired value.
adding a '0.' in front of whole numbers doesn't quite work as decimals
that is, unless your decimals never turn into whole numbers ;-)

Tis true. But personally I prefer to do decimal conversion myself,
whereby if the number has a decimal in it, instead of dividing by 10, I
would convert to string, move the decimal, convert back to number. Only
because it circumvents any chance of floating point division errors.
 
R

Robi

Randy said:
Robi said the following on 10/7/2005 9:05 PM:
Randy Webb wrote: [...]
document.write('<div style="font-size:0.' + i +
^^^^^^^
this isn't quite what hakim wants ;-)
it would only get from 0.1 to 0.9 and then start with 0.10, 0.11 until 0.50
and not as hakim coded it from 0.1 to 5 in .1 increments

YIKES!!!

:)

[...]
absolutely correct, but after counting in whole numbers divide
it by 10 to get the desired value.
adding a '0.' in front of whole numbers doesn't quite work as decimals
that is, unless your decimals never turn into whole numbers ;-)

Tis true. But personally I prefer to do decimal conversion myself,
whereby if the number has a decimal in it, instead of dividing by 10, I
would convert to string, move the decimal, convert back to number. Only
because it circumvents any chance of floating point division errors.

Floating poit errors ~= flops

as in "that equation was a flop"
or "that function was full of flops" ;-)

By moving the decimal you need to be careful or the above flop happens ]:->
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top