george samuel
george samuel

Reputation: 193

android how to change long to decimal and display the result

I am making an app. The app will calculate pmt function, its all working fine but the final result were not. The result could be displayed in int/long format but the request is to make it able to be displayed in currency format/decimal format that looks like currency.I am trying to display the results. However my implementation is bad and when the function called the app will crash.

editted:here is the code(just example):

   public class TableOutput extends Activity {
    TextView MyTextView ;
    String variable1;
    String todecimal;
    @Override
    protected void onCreate(Bundle bundle) {
        // TODO Auto-generated method stub
        super.onCreate(bundle);
        setContentView(R.layout.tableoutput);

        DecimalFormat formattedStuff = new DecimalFormat("#,###,###,###,##0");

        MyTextView = (TextView) findViewById(R.id.textview1);

        bundle = getIntent().getExtras();
        long CalculationResult = bundle.getLong("MyCalculationResultFromAnotherClass");

        variable1 = Long.toString(CalculationResult);

        todecimal = formattedStuff.format(variable1);

        MyTextView .setText(todecimal +" IDR");

    }       
}

the logcat:

02-26 16:46:26.438: E/AndroidRuntime(350): FATAL EXCEPTION: main
02-26 16:46:26.438: E/AndroidRuntime(350): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.m4nd1r1tun45f1n4nc3.abcdefg/com.m4nd1r1tun45f1n4nc3.abcdefg.TableOutput}: java.lang.IllegalArgumentException
02-26 16:46:26.438: E/AndroidRuntime(350):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-26 16:46:26.438: E/AndroidRuntime(350):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-26 16:46:26.438: E/AndroidRuntime(350):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-26 16:46:26.438: E/AndroidRuntime(350):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-26 16:46:26.438: E/AndroidRuntime(350):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 16:46:26.438: E/AndroidRuntime(350):  at android.os.Looper.loop(Looper.java:123)
02-26 16:46:26.438: E/AndroidRuntime(350):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-26 16:46:26.438: E/AndroidRuntime(350):  at java.lang.reflect.Method.invokeNative(Native Method)
02-26 16:46:26.438: E/AndroidRuntime(350):  at java.lang.reflect.Method.invoke(Method.java:521)
02-26 16:46:26.438: E/AndroidRuntime(350):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-26 16:46:26.438: E/AndroidRuntime(350):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-26 16:46:26.438: E/AndroidRuntime(350):  at dalvik.system.NativeStart.main(Native Method)
02-26 16:46:26.438: E/AndroidRuntime(350): Caused by: java.lang.IllegalArgumentException
02-26 16:46:26.438: E/AndroidRuntime(350):  at java.text.NumberFormat.format(NumberFormat.java:313)
02-26 16:46:26.438: E/AndroidRuntime(350):  at java.text.DecimalFormat.format(DecimalFormat.java:732)
02-26 16:46:26.438: E/AndroidRuntime(350):  at java.text.Format.format(Format.java:133)
02-26 16:46:26.438: E/AndroidRuntime(350):  at com.m4nd1r1tun45f1n4nc3.abcdefg.TableOutput.onCreate(TableOutput.java:121)
02-26 16:46:26.438: E/AndroidRuntime(350):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-26 16:46:26.438: E/AndroidRuntime(350):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-26 16:46:26.438: E/AndroidRuntime(350):  ... 11 more

Upvotes: 1

Views: 247

Answers (2)

Anil Jangir
Anil Jangir

Reputation: 19

Formater can't convert the string so use long or double.

public class TableOutput extends Activity {
TextView MyTextView ;
String variable1;
String todecimal;
@Override
protected void onCreate(Bundle bundle) {
    // TODO Auto-generated method stub
    super.onCreate(bundle);
    setContentView(R.layout.tableoutput);

    DecimalFormat formattedStuff = new DecimalFormat("#,###,###,###,##0");

    MyTextView = (TextView) findViewById(R.id.textview1);

    bundle = getIntent().getExtras();
    long CalculationResult = bundle.getLong("MyCalculationResultFromAnotherClass");

    variable1 = Long.toString(CalculationResult);

/*todecimal = formattedStuff.format(variable1);
 Formater can't convert the string so use long or double.
*/
todecimal = formattedStuff.format(CalculationResult);

    MyTextView .setText(todecimal +" IDR");

}       
}

Upvotes: 1

Android Developer
Android Developer

Reputation: 466

Try below code

public class TableOutput extends Activity {
TextView MyTextView ;
String variable1;
String todecimal;
@Override
protected void onCreate(Bundle bundle) {
    // TODO Auto-generated method stub
    super.onCreate(bundle);
    setContentView(R.layout.tableoutput);

    DecimalFormat formattedStuff = new DecimalFormat("#,###,###,###,##0");

    MyTextView = (TextView) findViewById(R.id.textview1);

    bundle = getIntent().getExtras();
    long CalculationResult = bundle.getLong("MyCalculationResultFromAnotherClass");

    //variable1 = Long.toString(CalculationResult);

    todecimal = formattedStuff.format(CalculationResult);

    MyTextView .setText(todecimal +" IDR");

}       

}

Upvotes: 1

Related Questions