Skip to content
Snippets Groups Projects
Commit 1a14d626 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

better format for numbers

parent 4bdb4917
No related branches found
No related tags found
No related merge requests found
package org.geuz.onelab; package org.geuz.onelab;
import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import android.content.Context; import android.content.Context;
...@@ -46,42 +45,40 @@ public class ParameterNumber extends Parameter{ ...@@ -46,42 +45,40 @@ public class ParameterNumber extends Parameter{
_readOnly = readOnly; _readOnly = readOnly;
} }
public static String formatDouble(double x)
{
return String.format("%.6g", x).replaceFirst("\\.?0+(e|$)", "$1");
}
protected void update() protected void update()
{ {
super.update(); super.update();
int nDecimal = String.valueOf(_min).length() - String.valueOf(_min).lastIndexOf('.') - 1; // hack for double round
if(_bar != null) { if(_bar != null) {
DecimalFormat df = new DecimalFormat ( ) ; _title.setText(getShortName() + " (" + formatDouble(_value)+ ")");
df.setMaximumFractionDigits(nDecimal) ;
_title.setText(getShortName() + " (" + df.format(_value)+ ")");
_bar.setMax(100); _bar.setMax(100);
_bar.setProgress((int)(100*(_value-_min)/(_max-_min))); _bar.setProgress((int)(100*(_value-_min)/(_max-_min)));
_bar.setEnabled(!this.isReadOnly()); _bar.setEnabled(!this.isReadOnly());
} }
else if(_spinner != null) else if(_spinner != null) {
{ for(int i=0;i<_choices.size();i++)
for(int i=0;i<_choices.size();i++) if(_values.get(i) == _value)
if(_values.get(i) == _value) _spinner.setSelection(i, true);
_spinner.setSelection(i, true); }
} else if(_checkbox != null) {
else if(_checkbox != null) _checkbox.setText(getShortName());
{ _checkbox.setChecked((_value == 0)? false : true);
_checkbox.setText(getShortName()); }
_checkbox.setChecked((_value == 0)? false : true); else if(_edittext != null) {
} _edittext.setText(""+formatDouble(_value));
else if(_edittext != null) }
{ else if(_stepper != null) {
_edittext.setText(""+Math.round(_value*Math.pow(10, nDecimal))/Math.pow(10, nDecimal)); _stepper.setMaximum((int)Math.round(_max));
} _stepper.setMinimum((int)Math.round(_min));
else if(_stepper != null) _stepper.setValue((int)Math.round(_value));
{ }
_stepper.setMaximum((int)Math.round(_max));
_stepper.setMinimum((int)Math.round(_min));
_stepper.setValue((int)Math.round(_value));
}
} }
public void setValue(double value)
public void setValue(double value) { {
if(value < _min || value > _max) { if(value < _min || value > _max) {
//Log.w("ParameterNumber", "Incorect value "+value+" (max="+_max+" min="+_min+")"); //Log.w("ParameterNumber", "Incorect value "+value+" (max="+_max+" min="+_min+")");
return; return;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment