Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying Currency Format Issue

First post. I'm brand new to software development in general and have spent hours trying to figure this piece out. As you can see, I'm converting a double to a String, then assigning that value to textResult (String). I formatted it properly to display decimals, but I can't figure out how to show as currency instead.

Based on what i've found online, it looks like I may have to use

NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

and then use nf.format() somehow but it just doesn't work for me. Any guidance would be greatly appreciated.

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

}
like image 384
Alex Avatar asked Feb 03 '26 19:02

Alex


2 Answers

There are two simple solutions to this. You can use a DecimalFormat object, or you can use a NumberFormat object.

I personally prefer a Decimalformat object because it gives you more precise control over how you would like to format your output value/text.

Some may prefer the NumberFormat object because the .getcurrencyInstance() method is easier to understand than a cryptic string format (e.g. "$#.00", "#0.00").

public static void main(String[] args) {
    Double currency = 123.4;
    DecimalFormat decF = new DecimalFormat("$#.00");
    
    System.out.println(decF.format(currency));

    Double numCurrency = 567.89;
    NumberFormat numFor = NumberFormat.getCurrencyInstance();
    System.out.println(numFor.format(numCurrency));
}

The output for this example program is below:

$123.40

$567.89

like image 144
Blake Yarbrough Avatar answered Feb 06 '26 07:02

Blake Yarbrough


You need to use formatter to format the double value you want, eg:

double money = 202.2
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String moneyString = formatter.format(money);
System.out.println(moneyString);
like image 26
Kris Avatar answered Feb 06 '26 07:02

Kris