第一篇文章。我一般都是軟件開發方面的新手,並花了數小時的時間試圖弄清楚這件事。如您所見,我將double
轉換爲String
,然後將該值分配給textResult
(String
)。我正確格式化它以顯示小數,但我無法弄清楚如何顯示爲貨幣。顯示貨幣格式問題
基於我已經在網上找到的,它看起來像我可能不得不使用
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
然後用nf.format()
莫名其妙,但它只是不爲我工作。任何指導將不勝感激。
public void onCalculateDealOne(View v) {
//get values from text fields
EditText priceEntry = (EditText) findViewById(R.id.etListPriceDealOne);
EditText unitsEntry = (EditText) findViewById(R.id.etNumberOfUnitsDealOne);
EditText couponEntry = (EditText) findViewById(R.id.etCouponAmountDealOne);
//get value from result label
TextView result = (TextView) findViewById(R.id.perUnitCostDealOne);
//assign entered values to int variables
double price = Double.parseDouble(priceEntry.getText().toString());
double units = Double.parseDouble(unitsEntry.getText().toString());
double coupon = Double.parseDouble(couponEntry.getText().toString());
//create variable that holds the calculated result and then do the math
double calculatedResultDealOne = (price - coupon)/units;
//convert calculatedResult to string
String textResult = String.format("%.3f", calculatedResultDealOne);
result.setText(textResult + " per unit");
dealOneValue = calculatedResultDealOne;
//hide the keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//make deal one label visible
result.setVisibility(View.VISIBLE);
}