yourTextView.setText(String.format("Value of a: %.2f", a));
Answer from Goz on Stack OverflowyourTextView.setText(String.format("Value of a: %.2f", a));
For Displaying digit upto two decimal places there are two possibilities - 1) Firstly, you only want to display decimal digits if it's there. For example - i) 12.10 to be displayed as 12.1, ii) 12.00 to be displayed as 12. Then use-
DecimalFormat formater = new DecimalFormat("#.##");
2) Secondly, you want to display decimal digits irrespective of decimal present For example -i) 12.10 to be displayed as 12.10. ii) 12 to be displayed as 12.00.Then use-
DecimalFormat formater = new DecimalFormat("0.00");
Remember you can't use
String.format("%.2f", maltRequiredString);
Because maltRequiredString is string. Correct coding is that you must use float in this function and for that you have to convert your string to float
float f = Float.valueOf(maltRequiredString);
String test = String.format("%.02f", f);
You can also use this technique for making it 2 decimal
DecimalFormat decimalFormat = new DecimalFormat("#.##");
float twoDigitsF = Float.valueOf(decimalFormat.format(f));
You can use DecimalFormat to overcome this problem.
//Make a new instance df that has 2 decimal places pattern.
Decimalformat df = new DecimalFormat("#.##");
// converting maltRequired form double to string.
String maltRequiredString = df.format(maltRequired);
// then set the value of maltRequiredString to the TextView.
maltRequiredTextView.setText(String.valueOf(maltRequiredString));
DecimalFormat formatter = new DecimalFormat("00");
String minformat = formatter.format(text);
NumberFormat class will help you in achieving this.
NumberFormat nf = NumberFormat.getInstance();
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
Then you set text using format() method: myTextView.setText(nf.format(myText));
You also might want to check input format as user types in an EditText. To do this you should addTextChangedListener(TextWatcher watcher) and listen to user input event.
this could solve your problem...
temp = 0.6044610142707825;
new DecimalFormat("###")
.format(temp*1000); //converts to 604.4610142707825 and then to 604;
Try decimalformat
// Fetch only the first rounded off digit after decimal.
String abc = new DecimalFormat("0.#").format(0.604461014);
// Converting to integer and multiply 1000
int myFinalNo = (int) ( Double.parseDouble(abc) * 1000);
// Returns output 600
Note:
String abc = new DecimalFormat("0.#").format(0.674461014);
int myFinalNo = (int) ( Double.parseDouble(abc) * 1000);
// Returns output 700
If you DO NOT WANT ROUNDING, then use:
DecimalFormat df = new DecimalFormat("0.#");
df.setRoundingMode(RoundingMode.DOWN);
String abc = df.format(0.684461014);
int myFinalNo = (int) ( Double.parseDouble(abc) * 1000);
System.out.println ("abc="+abc + "** myFinalNo="+myFinalNo);
You can accomplish it with DecimalFormat:
NumberFormat f = new DecimalFormat("00");
long time = 9;
textView.setText(f.format(time));
Output:
09
Or you can use String.format() as well:
String format = "%1$02d"; // two digits
textView.setText(String.format(format, time));
Use: text.setText(String.format("%02d", i)); where i is the integer value
I think the main problem is that you are setting a text to the EditText inside of a TextWatcher which leads to looping recursion and then stack-overflow. You should change the text wrapped in removing and adding again the TextWatcher. Here is a simple solution:
editTextField.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
print("beforeTextChanged")
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
print("onTextChanged")
val newValue = s.toString()
.takeIf { it.isNotBlank() }
?.replace(".", "")
?.toDouble() ?: 0.0
editTextField.let {
it.removeTextChangedListener(this)
it.setText(String.format("%.2f", newValue / 100))
it.setSelection(it.text?.length ?: 0)
it.addTextChangedListener(this)
}
}
override fun afterTextChanged(s: Editable?) {
print("afterTextChanged")
}
})
For Kotlin
fun roundOffDecimal(number: Double): String? {
val df = DecimalFormat("#,###,###.##")
df.roundingMode = RoundingMode.CEILING
return df.format(number)
}
RoundingMode.CEILNG or RoundingMode.FLOOR is used to round up the last digit.
#,###,###.##
customize this part according to the place value type you need and the number of decimal digits you want.
The above code will show the result something similar to 3,250,250.12
You can try this,
DecimalFormat format= new DecimalFormat("#.0");
td = format.format(totalDistance / 1000.0);
Or refer this answer which is for rounding numbers.
Use DecimalFormat. You can also set RoundingMode to get the ceiling for last digit . Below is an example .
double x=2.7883;
DecimalFormat df = new DecimalFormat("#.0");
System.out.println(df.format(x));
Output:- 2.8
For setting mode you can use .
df.setRoundingMode(RoundingMode.FLOOR);
This is the format you need:
val dec = DecimalFormat("#,###.##")
will print:
5.384,45
if you need always exactly 2 digits after the decimal point:
val dec = DecimalFormat("#,###.00")
The "most Kotlin-esque" way I found to do this sort of formatting is:
"%,.2f".format(Locale.GERMAN, 1234.5678) // => "1.234,57"
"%,.2f".format(Locale.ENGLISH, 1234.5678) // => "1,234.57"
"%,.2f".format(1234.5678) // => "1,234.57" for me, in en_AU
Note though that even though this is Kotlin's own extension method on String, it still only works on the JVM.
For those looking for a multiplatform implementation (as I was), mp_stools is one option.