NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
April 21, 2021, 08:27:59 am

Know the universe as yourself and you can live anywhere in comfort. ...Lao Tzu (570-490 BC)

 Pages: [1]   Go Down
 Author Topic: how to make a Ejs way to recognize that a number is 10,100,1000 etc  (Read 6863 times) 0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message on: March 28, 2009, 11:10:47 am »

how to make a Ejs way to recognize that a number is 10,100,1000 etc

if
number is 1000 to 9999 , how to display 1000 ?

so if a number is 100 to 999, how to display 100 ?

if the number is 10 to 99, how to display 10

number 0 to 9 display as 1 ?

thanks!
 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3086

 « Embed this message Reply #1 on: March 28, 2009, 10:22:38 pm »

Your question is not limited to EJS. It is a general question related to mathematics and programming.

3≦log10(x)<4 if 10002≦log10(x)<3 if 100
However, java did not provide function for log10(x).
It only provide nature log, i.e. ln(x).
log10(x)= loge(x)/loge(10)

and Math.pow(a,b) will return ab

The following formula should give you what you want.
Code:
y=(int)(Math.pow(10,(int)(Math.log(x)/Math.log(10.)+1.e-8)));
1.e-8 was added due to possible numerical error.

 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #2 on: March 30, 2009, 10:24:15 am »

excellent help.

Code:
scaffold=(int)(Math.pow(10,(int)(Math.log(i2)/Math.log(10.)+1.e-8)));

i assign my own variables ( scaffold and i2) substitution to modify.

thanks for the programming tip.

i read a bit before on Math.pow(a,b) which is ab

so what you suggested is 10(i2/log10 +1.e-8)
 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3086

 « Embed this message Reply #3 on: March 30, 2009, 10:37:50 am »

What I suggest is 10(log(i2)/log(10))+1.e-8
 Logged
 Pages: [1]   Go Up
Know the universe as yourself and you can live anywhere in comfort. ...Lao Tzu (570-490 BC)