Truncate value after decimal point in android - android

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)

Related

Create German currency format

I'm going to create a German currency format in Android, I just can't get the thousand, million, separator work. This is what I did so far:
String bill_subtotal = String.format("%.2f", bill_amount);
txtSubtotal = (TextView) findViewById(R.id.txtSubtotal);
txtSubtotal.setText(String.valueOf(bill_subtotal).replace(',', '.'));
Those above only make values like this: 60000.50 into 60000,50
I want to make it 60.000,50
Is there a way to make it like that?
My solution is ,
public static String formatGermanCurrency(double number) {
NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY);
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
return nf.format(number);
}

(android) decimalformat, uses comma in place of fullstop while formatting with "#.##"

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.

How to show a double formatted as a price in a textview in android

I want to show in a textView a price value like: 12,199.99 in a textview in Android. but I have the value stored in a double variable (12199.99). Is there any way to show that double in the textview in the format I want?
If you want to format it using the phone's locale with 2 decimals do like this:
DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");
String formattedValue = decimalFormat.format(yourDoubleValue);
yourTextView.setText(formattedValue);
Hope it helps.
You can use a BigDecimal for it, for example
public static BigDecimal doubleToScale(double d, int scale){
return new BigDecimal(d).setScale(scale, BigDecimal.ROUND_HALF_UP);
}
I hope that helps.
Use String.format. More documentation on the formatting options can be found here.

only reading till 1st decimal digit

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

Convert String to Double excluding part of the string

How to convert double to a string value that I get from the spinner, for example: "10 (ton)."
In summary:
How to convert string to double and delete the part (ton)?
I need only the value 10
Another option is using Java's substring method in the String class.
The signature is:
substring(int beginIndex, int endIndex)
Where endIndex equals to the index of the last character you want to include + 1.
In the case of your example, it will look like this:
String myString = "10 (ton)";
Double dbl = Double.parseDouble(myString.substring(0, 2));
Here is the link to the method:
Java substring
You must parse this String. Here is some example. Also use search.
Check these methods Double.parseDouble() and Double.toString() use these functions for converting double to string or vice-versa.
first you have to get rid of "(ton)" which can be achieved by using a String method for example
String inputString = "10 (ton)";
String str = inputString.split(" ")[0];
After that just parse the double Value
Double dbl = Double.parseDouble(str);
BTW: Not sure whether you want to go from double to string or vice-versa
i have a similar problem.
for correct formatting EditText text content to double value i use this code:
try {
String eAm = etAmount.getText().toString();
DecimalFormat dF = new DecimalFormat("0.00");
Number num = dF.parse(eAm);
mPayContext.amount = num.doubleValue();
} catch (Exception e) {
mPayContext.amount = 0.0d;
}
this is independet from current phone locale and return correct double value.
hope it's help;

Categories

Resources