NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
September 25, 2020, 05:21:20 am

"Choose a job you love, and you will not have to work for a day in your life." ...Confucius (551-479 BC)

 Pages: [1]   Go Down
 Author Topic: in ejs how to show to number as a power of 10?  (Read 11066 times) 0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
Fred Chuit
Newbie

Offline

Posts: 18

 « Embed this message 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"."?

It would be great if I could get some simple examples to solve this problems.
Already many thanks for the help.
Greetings,
Fred
 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3085

 « Embed this message Reply #1 on: July 23, 2008, 01:37:22 pm » posted from:Taipei,T\'ai-pei,Taiwan

Registed user can get files related to this applet for offline access.
Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list

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).

Registed user can get files related to this applet for offline access.
Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
 Logged
Fred Chuit
Newbie

Offline

Posts: 18

 « Embed this message Reply #2 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.