NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Easy Java Simulations (2001- ) => misc => Topic started by: Fred Chuit on July 23, 2008, 01:39:08 am



Title: in ejs how to show to number as a power of 10?
Post by: Fred Chuit on July 23, 2008, 01:39:08 am
Dear Sir,

I am just starting to build applets with ejs, it's a wonderful program.
I would like some numbers (double) to be shown on screen as a power of 10.
For instance:
23000 as: 2,3•10^4  (if possible 4 as exponent)
0,00356 as: 3,6•10^-3 (-3 as exponent).

Further, is there a way to have a numberfield also accept(and show) the "," instead of the"."?
So: 34,5 instead of 34.5

It would be great if I could get some simple examples to solve this problems.
Already many thanks for the help.
Greetings,
Fred


Title: Re: in ejs how to show to number as a power of 10?
Post by: Fu-Kwun Hwang on July 23, 2008, 01:37:22 pm
This is what I will do: define a function to convert your value to a string.

For example: define variables in the model
e (double): 1.602e-19
s (String): d2s(v)    // to be used to show e as a String

public String d2s(double v){
 double a;
 int b;
 String o;
 b=(int)(Math.log(v)/Math.log(10.)); // get the value as power of 10
 if(b<0)b=b-1;
 a=d2v(v/Math.pow(10,b));
 o=a+"";
 return o.replace(".",",")+"*10^{"+b+"}";  // convert "." to ","
}
public double d2v(double v){//  for 3 digits displayed max.
 double dv;
 if(v>0)dv=0.5;
 else if(v<0)dv=-0.5;
 else dv=0;
 return (int)(v*1000+dv)/1000.;
}

Add a Text element into DrawingPanel, and set it's text to %s%
You should be able to find 1.602*10-19 in the drawingPanel when you run it.

EJS support (^ or _) for  (superscript or subscript) . Try it at title for PlotingPanel, text element. etc.

I also found "." will be switch to "," for value displayed in slider/Bar/...  when you switch locale from en_EN to es_ES. (I think it will work the same if you select the right locale before running EJS).





Title: Re: in ejs how to show to number as a power of 10?
Post by: Fred Chuit on July 23, 2008, 03:18:56 pm
Dear mr. Fu-Kwun Hwang,

Many thanks for you advice and help!
This really solves the problem, I couldn't fix.

Again, thanks for your fast reply!
Greetings from Holland,
Fred