Skip to content

Instantly share code, notes, and snippets.

Created April 28, 2015 20:34
Show Gist options
  • Select an option

  • Save anonymous/fa134c55a4a43e8d6004 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/fa134c55a4a43e8d6004 to your computer and use it in GitHub Desktop.
/**
* This method displays the given price on the screen.
*/
private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}
@alf-win

alf-win commented Jun 15, 2021

Copy link
Copy Markdown

Screenshot 2021-06-15 at 11 39 41 PM

Screenshot 2021-06-15 at 11 49 21 PM

please help !

@simarjeet30

Copy link
Copy Markdown

@chekwon78 Do try adding a TextView in activity_main.xml and assign it an id like this android:id="@+id/price_text_view".

Thanks soo much ,it worked for me

@Kgarmel

Kgarmel commented Jul 25, 2021

Copy link
Copy Markdown

Hello,
Need your help to solve the numberFormat error after adding the "Add unambiguous imports on the fly"

image

@Kgarmel

Kgarmel commented Jul 25, 2021

Copy link
Copy Markdown

It's ok, solved by adding "import java.text.NumberFormat;" . thanks.

@artjomHeim

Copy link
Copy Markdown

grafik

error: invalid method declaration; return type required
} displayPrice(2*5);
^

@saiprajoth

Copy link
Copy Markdown

don't why my app is crashing after clicking on the ORDER button

@shashankagrawal158

Copy link
Copy Markdown

error
How do I resolve this error?

@saurav-ux

Copy link
Copy Markdown

Code of [chetan081] approach

private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price);
priceTextView.setText("₹ "+ number);
}

@noah12binche

Copy link
Copy Markdown

android_errer
How do i resolve this error

@Saphiriela

Copy link
Copy Markdown

After latest update of Android studio it should be this code in Kotlin:

/**
* This method displays the given price on the screen.
*/
private fun displayPrice(number: Int) {
val priceTextView = findViewById(com.example.justjava.R.id.price_text_view) as TextView
priceTextView.setText(java.text.NumberFormat.getCurrencyInstance().format(number))
}

@Selamyhun

Copy link
Copy Markdown

Code segment works fine the problem is that the emulator got crushed every time after running the program:
private void displayPrice(int number) {
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));
}

@hayleeyesusshiferaw

Copy link
Copy Markdown

ቅጽበታዊ ዕይታ (35)
i have problem is my java code how can i correct since i am new for the code

@Bekal-Max-dot

Copy link
Copy Markdown

For all of those who are trying to get currency in their locale this will help

The lines of code you see below is exactly the same as display method the one you see in quantity_text_view

private void displayPrice(int number) { TextView priceTextView = (TextView) findViewById(R.id.price_text_view); priceTextView.setText(NumberFormat.getCurrencyInstance().format(number)); }

The major difference here is this line priceTextView.setText(NumberFormat.getCurrencyInstance().format(number));

Now what exactly this line of code does is,it's adding the currecy symbol to your calculation like this $10.00

This whole line is just to get currency sign **(NumberFormat.getCurrencyInstance().format()

Now to change currency to your locale there are two ways:

Easy Way

  1. priceTextView.setText("₹"+number);

In this you provide your currency symbol directly inside these "" marks

This line is exactly same as one you see in display method but here your adding your currency sign to the number

In this way you don't get decimal numbers what i mean is you get like this ₹10 instead of this ₹10.00

Udacity Way

  1. Like i said above

This whole line is just to get currency sign (NumberFormat.getCurrencyInstance().format(number).

All we have to do is change it to your local like this

priceTextView.setText(NumberFormat.getCurrencyInstance(new Locale("en","IN")).format(number));

Above i've added this new Locale("en","IN") new line inside of getCurrencyInstance()

By adding that line we are saying i want the currency to be in other local instead of default one with this new Locale()

Now we need to tell in what local we want the currency to be.

We say that by adding these "en" , "in"

  • "en" -This is language and i want it to be in ENGLISH.
  • "IN" -This is country since i'm INDIAN i say "IN"

HOPE THIS WILL HELP , THANK YOU!

Thank you! It works.

@sileshi-21

sileshi-21 commented Oct 29, 2024

Copy link
Copy Markdown

dithub
it dos't work this cod how can fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment