I have to display digit in two decimal points.
Let say value 0 should display as 0.00 and value 2.3 shoule display as 2.30.
To achive this I have done something like below :
Log.e(">>> percent ", ">> " + data.percent)
percent = String.format("%.2f", percent).toDouble()
Log.e(">>> percent ", ">> " + percent)
But the result you will notice is as below :
2021-04-15 11:55:54.580 10061-15047/com.dev E/>>> percent: >> 0
2021-04-15 11:55:54.580 10061-15047/com.dev E/>>> percent: >> 0.0
It should be 0.00 instead of 0.0
What might be the issue?
Please guide. Thanks.
Double type does not have information about how to display.
So, do not use toDouble() method to the string.
Also, add 0 to the format string to denote the zero padding.
Log.e(">>> percent ", ">> " + data.percent)
val percentStr: String = String.format("%.02f", data.percent)
Log.e(">>> percent ", ">> " + percentStr)
You can use DecimalFormat https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
val df = DecimalFormat("0.00")
df.format(percent)
just change
String.format("%.2f", percent).toDouble()
to
String.format("%.02f", data.percent)
Related
my double = 42.12323532 I want it to show just "42". How do I do that? This is what I have tried-
TV_BMR.setText("BMR: " + String.format("%.2f", BMR) + " cal");
but this only rounds it to 2 digits. I want everything removed after the decimal. Including the decimal.
String str = “BMR: “ + (int)BMR;
String str = “BMR: “ + (long)BMR;
Then we can, In the high double type range numbers everything removed after the decimal.
I'm in the middle of developping a map app that enables user to send duration data that it takes him to get to another place, to the database (MySQL) , for this reason I tried to extract the double value of duration directely from url as it's showing in the method below :
#Override
public void setDouble(String result) {
String res[]=result.split(",");
Double min=Double.parseDouble(res[0])/60;
int dist=Integer.parseInt(res[1])/1000;
Duree.setText("Duration= " + (int) (min / 60) + " hr " + (int) (min % 60) + " mins");
Distance.setText("Distance= " + dist + " kilometers");
}
In this method it worked, but when I tried to do it like this :
url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins="+latt+","+lngg+"&destinations="+lt+","+lg+"&mode=driving&language=fr-FR&avoid=tolls&key=API_KEY";
String res[]=url.split(",");
Double duration=Double.parseDouble(res[0])/60;
It showed me that it's not a valid double value, knowing that I need to send this value when I click a button showed on a popup window after marker click (So the problem of inner class is posed).
Can you help me know what is the right way to do it ,if that is possible !
Don't expect your first result-field contains the duration but make your parsing a little more intelligent. Use, by example, the org.json library (introduction on json
Following code snippet should help a little:
LOGGER.debug("---google duration in seconds = {}", jsonElement.opt("duration") == null ? 0 : jsonElement.getJSONObject("duration").get("value"));
transportInfo.setDuration(jsonElement.opt("duration") == null ? 0 : (Integer) jsonElement.getJSONObject("duration").get("value"));
And, btw, I would recommend removing your API key ASAP from your post .... Be aware this key could be used by other people.
Example :
value = 0.0
I want to covert it to
Ans :value = 00.0
example
value = 12.0
Ans: 12.0
example
value = 1.0
Ans: 01.0
Right now for float values i am using String.format( " %.2f" ,variable_value)
is there any way like this to convert values
Thanks
you can use string.format method like below:
String value= "_%02d" + "_%s";
it converts numbers like :
String.format("%02d", 1); // => "01"
you can see forrmatter doc for other modifiers
Can anybody tell me why the result of the following expression is -4.91679930495
9.81*Math.cos(phi)/Math.sin(phi)
if phi = 90°?
In particular, I want to display the value of acceleration.
tvText2.setText("acceleration: " + String.valueOf(9.81*Math.cos(phi)/Math.sin(phi)) + " m/s²");
Acording to docs http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#cos(double):
The paremeter of those methods is in radians.
you will need to call Math.toRadians that receives an angle in degrees. http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#toRadians(double)
Convert phi to Radian before calculating acceleration:
double phiInRad = Math.toRadians(phi);
tvText2.setText("acceleration: " + String.valueOf(9.81*Math.cos(phiInRad)/Math.sin(phiInRad)) + " m/s²");
or in one line
tvText2.setText("acceleration: " + String.valueOf(9.81*Math.cos(Math.toRadians(phi))/Math.sin(Math.toRadians(phi))) + " m/s²");
TextView.setText(XMLdata.get("XMLField") + " mm");
input values that are expressed in an abbreviated way:
10,5858555966668 (with comma)
Which I would like to convert into:
10,58
if your XMLdata.get("XMLField") value is float u can do the following code
String finalValue = String.format(
"%.2f", XMLdata.get("XMLField"));
TextView.setText(finalValue + " mm");
if your XMLdata.get("XMLField") is String convert it into float do the above method.
TextView.setText((Math.floor(MathXMLdata.get("XMLField") * 100.0) / 100.0).toString() + " mm");
This converts 10,5858555966668 to 1058,58555966668, rounds it to 1058 and converts it back to 10,58.
Note: Math.floor always rounds down (cuts off all missing fractional numbers). If you intend to round to the closest value, use Math.round.
Thank you, thank you very much rmkrishna and Faro ((= I do it with
TextView.setText(String.format("%.2f",Double.parseDouble(XMLData.get("Son7GunlukSagis").replace(",", ".")))+ " mm");