In my android app, I have an EditText in which the user enters a decimal which can be as long as the user wants. It can be a number like 25, 54.77, 23.7, 7.88, etc. In the same activity, I have a textView which reads the input into the EditText and displays the decimal but only till the first decimal digit. For e.g. if the user enters: 25 should be displayed as 25, 54.77 as 54.7, 23.7 as 23.7, 7.88 as 7.8. How can I achieve this? I tried using the following code but it didn't work:
NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(0);
df.setMaximumFractionDigits(1);
df.format(EditTextNumber);
Use String.format:
String result = String.format("%.1f", yourdecimalvalue);
you can use DecimalFormat too:
NumberFormat formatter = new DecimalFormat("#0.0");
String result = formatter.format(yourdeciamlvalue);
Try NumberFormat instead of DecimalFormat:
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(1);
TextView.setText(nf.format(editText.getText()));
if you are grbbing the input via an edittext then u can use this code
String number=input.getText().toString(); //input is ur input edittext
String output=number;
int indexOfDecimal=number.lastIndexOf('.');
if(indexOfDecimal!=-1&&indexOfDecimal<output.length()-1)
number=output.substring(0, indexOfDecimal+2); // use number as the output
Related
I want to truncate the value from the given value 32.1500 into 32 and display it on textview text please help me out i searched a lot but did not found anything.
If you don't want to cast it you can keep it at double but truncate the trailing zeroes like this:
textView.setText(String.format("%.0f", 5.222));
Try to use cast like this.
double number=32.1500;
int numbereditted=(int) number;
textView.setText("Number= "+numbereditted);
Or use decimal format.
double number=32.1500;
DecimalFormat df=new DecimalFormat("0");
textView.setText("Number= "+df.format(number));
Try something like this:
private final DecimalFormat decimalFormat = new DecimalFormat("#.####");
private final DecimalFormat noDecimalFormat = new DecimalFormat("#");
String decimalValue = decimalFormat.format(value);
String noDecimalValue = noDecimalFormat.format(value);
Or using NumberFormat instead of DecimalFormat:
private NumberFormat formatter;
// Set the properties for the number format that will display the values
formatter = NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(4);
formatter.setMaximumFractionDigits(4);
String decimalValue = formatter.format(value);
formatter.setMinimumFractionDigits(0);
formatter.setMaximumFractionDigits(0);
String noDecimalValue = formatter.format(value);
You can set the rounding mode you want like this:
noDecimalFormat.setRoundingMode(RoundingMode.DOWN)
formatter.setRoundingMode(RoundingMode.DOWN)
I am developing an Android app where I want to format my double number to #.##, which I have done using below code.
Double BMI = ((fWeight)/(dHeight*dHeight));
DecimalFormat df = new DecimalFormat("0.00");
String sBMI = df.format(BMI);
While testing when the language of hardware is set to English(default language), it works fine, for example if BMI is 2497.227216676656 , it formats sBMI to 2497.23 , but if language is selected to French it formats it to 2497,23. In place of DOT, COMMA is being used which is crashing my app!!!
What is the reason for this?
Try this:
Double BMI = ((fWeight)/(dHeight*dHeight));
DecimalFormat df = new DecimalFormat("0.00");
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
df.setDecimalFormatSymbols(dfs);
String sBMI = df.format(BMI);
You should use the factory static method and not the constructor in order to get the right format.
DecimalFormat df = DecimalFormat.getInstance(Locale.US);
Using US locale will make sure that all formatted data will be in the right form.
I'm trying to dynamically format the value entered by the user on a TextView, specifically what I am trying to do is:
1. TextView text = ""
2. User entered = "4"
3. TextView text = 0.04
4. User entered = "5"
5. TextView text = 0.45
6. User entered = "6"
7. TextView text = 4.56
and so on...
I'm using the onTextChangedmethod stub from TextWatcher, although I don't know how to implement this logic.
What is the simplest way to implement this feature?
You have to use NumberFormat in this case, e.g:
double moneyCurrency = 100.1;
NumberFormat baseFormat = NumberFormat.getCurrencyInstance();
String moneyString = baseFormat.format(moneyCurrency);
I have a the next line :
String.format("%10.6f",transform);
I use String.format to force the value to be shown with 6 decimals and not with E.
But when transform equals to 0.040000 I want to have 0.04 as result. And for 0.000006 I want to have 0.000006.
Use NumberFormat instead of String.format():
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(6);
nf.format(yourNumber);
In my android app, I am getting the String from an Edit Text and using it as a parameter to call a web service and fetch JSON data.
Now, the method I use for getting the String value from Edit Text is like this :
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
Now normally it works fine, but if we the text in Edit Text contains space then my app crashes.
for eg. - if someone types "food" in the Edit Text Box, then it's OK
but if somebody types "Indian food" it crashes.
How to remove spaces and get just the String ?
Isn't that just Java?
String k = edittext.getText().toString().replace(" ", "");
try this...
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
String newData = k.replaceAll(" ", "%20");
and use "newData"
String email=recEmail.getText().toString().trim();
String password=recPassword.getText().toString().trim();
In the future, I highly recommend checking the Java String methods in the API. It's a lifeline to getting the most out of your Java environment.
You can easily remove all white spaces using something like this. But you'll face another serious problem if you just do that. For example if you have input
String input1 = "aa bb cc"; // output aabbcc
String input2 = "a abbcc"; // output aabbcc
String input3 = "aabb cc"; // output aabbcc
One solution will be to fix your application to accept white spaces in input string or use some other literal to replace the white spaces. If you are using only alphanumeric values you do something like this
String input1 = "aa bb cc"; // aa_bb_cc
String input2 = "a abbcc"; //a_abbcc
String input3 = "aabb cc"; //aabb_cc
And after all if you are don' caring about the loose of information you can use any approach you want.