Java Tutorial/I18N/ChoiceFormat
Содержание
Choice Format
import java.text.ChoiceFormat;
public class ChoiceFormatApp {
public static void main(String args[]) {
double limits[] = { 0.0, 0.1, 0.3, 0.7 };
String labels[] = { "very low", "low", "moderate", "high" };
ChoiceFormat format = new ChoiceFormat(limits, labels);
double r = Math.random();
System.out.println(format.format(r) + " (" + r + ").");
}
}
//high (0.755370803805559).
Get the smallest double greater than d
import java.text.ChoiceFormat;
public class Main {
public static void main(String[] argv) throws Exception {
double d = 1.2;
double d2 = ChoiceFormat.nextDouble(d);
System.out.println(d2);
}
}
//1.2000000000000002
Implementing a Simple Event Notifier
import java.util.Observable;
import java.util.Observer;
class MyModel extends Observable {
public synchronized void setChanged() {
super.setChanged();
}
}
public class Main {
public static void main(String[] argv) throws Exception {
MyModel model = new MyModel();
model.addObserver(new Observer() {
public void update(Observable o, Object arg) {
}
});
model.setChanged();
Object arg = "new information";
model.notifyObservers(arg);
}
}
Incrementing a Double by the Smallest Possible Amount
import java.text.ChoiceFormat;
public class Main {
public static void main(String[] argv) throws Exception {
double d = 1.2;
// Get the largest double less than d
double d1 = ChoiceFormat.previousDouble(d);
System.out.println(d1);
}
}
//1.1999999999999997