Need to round off two points after decimal digit - android

I'm working on an app and facing an issue. I've tried a number of solutions but nothing solved my problem.
I need to round off two digits after decimal point.
For Example.
9.225 should be rounded off to 9.23
Thank you.

For Kotlin use "%.2f".format(number), for Java use String.format("%.2f", number)
Result:

You can use String.format("%.2f", d), this will rounded automatically. d is your value.
OR
You can use this
double d = 1.234567;
DecimalFormat df = new DecimalFormat("#.##");
Log.d(df.format(d));
You can get as a float value as well like below.
float value = Float.valueOf(df.format(d)); // Output will be 1.24

I would have gone with a probable over the top solution however this is what i came up with.
It uses regex to split the string value of the number passed and then rounds up/down depending on the leading digit after the decimal place. It will return a Double in the instance but you can change that if you like. It does throw IllegalArgumentException, but thats taste dependant.
/**
* #param value the value that is being transformed
* #param decimalPlace the decimal place you want to return to
* #return transformed value to the decimal place
* #throws IllegalArgumentException
*/
Double roundNumber(#NonNull Double value, #NonNull Integer decimalPlace) throws IllegalArgumentException {
String valueString = value.toString();
if(valueString.length()> decimalPlace+1){
throw new IllegalArgumentException(String.format("The string value of %s is not long enough to have %dplaces", valueString, decimalPlace));
}
Pattern pattern = Pattern.compile("(\\d)('.')(\\d)");
Matcher matcher = pattern.matcher(valueString);
if (matcher.groupCount() != 4) { //0 = entire pattern, so 4 should be the total ?
throw new IllegalArgumentException(String.format("The string value of %s does not contain three groups.", valueString));
}
String decimal = matcher.group(3);
int place = decimal.charAt(decimalPlace);
int afterDecimalPlace = decimal.charAt(decimalPlace + 1);
String newDecimal = decimal.substring(0, decimalPlace - 1);
newDecimal += afterDecimalPlace > 5 ? (place + 1) : place;
return Double.parseDouble(matcher.group(1) + "." + newDecimal);
}

Related

java.lang.NumberFormatException: Invalid double

I have a method to calculate Difference between two Latitude:
public static double distanceKM(LatLng latLng1, LatLng latLng2) {
int EARTH_RADIUS_KM = 6371;
double lat1Rad = Math.toRadians(latLng1.latitude);
double lat2Rad = Math.toRadians(latLng2.latitude);
double deltaLonRad = Math.toRadians(latLng2.longitude - latLng1.longitude);
double dist_travelled = Math
.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad)
* Math.cos(lat2Rad) * Math.cos(deltaLonRad))
* EARTH_RADIUS_KM;
dist_travelled = Double.parseDouble(new DecimalFormat("##.######")
.format(dist_travelled));
return dist_travelled;
}
Sometimes, this method throw Exception (I say sometimes, when I test in defference device):
java.lang.NumberFormatException: Invalid double: "0,179927"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:269)
Can someone help me in this case? Thanks
The value of the double depends on the language of the device. For example, for devices in french the number 0.179927 becomes 0,179927 which will always throw a NumberFormatException when parsing it to double because of the comma.
You need to change the separator from a comma to a point.
You can change the separator either by setting a locale or using the DecimalFormatSymbols.
If you want the grouping separator to be a point, you can use a european locale:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN);
DecimalFormat df = (DecimalFormat)nf;
Alternatively you can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers produced by the format method. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others:
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);

Is language conditional in some operations?

I'm developing an app where I use the Geocoder to get a place's coordinates.
The operative is this:
The user defines an address.
The geocoder finds that address and I get the coordinates from that address.
This coordinates are in decimal format and I need them in degrees-minutos so I format them.
To format the coordinates from decimal to degrees-minutes I use:
String frmtLatitude = Location.convert(Double.parseDouble(lat), Location.FORMAT_MINUTES);
So, if I have for example this latitude 43.249591 in decimal value, it returns it like this 43:14.97546.
After this, I have to make some operations to finally get the latitude with this appearance: 4314.975
When I do this operations, one of them is to split the value using the ".". I split 14.97546 to get in one hand the 14 and in the other 97546.
Until here, everything ok. It works fine when I have my phone's language selected to be in english. But if I select to be in spanish, the app crashes. I have followed the stacktrace and it points there. Is like that in english when using the first commented function to convert from decimal to degrees-minutes it separates the decimals with a "." but if I have it in spanish, it separates them with a ",".
Can this really happen or the cause could be another thing?
We can look at the source code of the convert method
public static String convert(double coordinate, int outputType) {
if (coordinate < -180.0 || coordinate > 180.0 ||
Double.isNaN(coordinate)) {
throw new IllegalArgumentException("coordinate=" + coordinate);
}
if ((outputType != FORMAT_DEGREES) &&
(outputType != FORMAT_MINUTES) &&
(outputType != FORMAT_SECONDS)) {
throw new IllegalArgumentException("outputType=" + outputType);
}
StringBuilder sb = new StringBuilder();
// Handle negative values
if (coordinate < 0) {
sb.append('-');
coordinate = -coordinate;
}
DecimalFormat df = new DecimalFormat("###.#####");
if (outputType == FORMAT_MINUTES || outputType == FORMAT_SECONDS) {
int degrees = (int) Math.floor(coordinate);
sb.append(degrees);
sb.append(':');
coordinate -= degrees;
coordinate *= 60.0;
if (outputType == FORMAT_SECONDS) {
int minutes = (int) Math.floor(coordinate);
sb.append(minutes);
sb.append(':');
coordinate -= minutes;
coordinate *= 60.0;
}
}
sb.append(df.format(coordinate));
return sb.toString();
}
We can see that it uses a DecimalFormat with a given pattern. So, if we look to the DecimalFormat constructor :
public DecimalFormat(String pattern) {
// Always applyPattern after the symbols are set
this.symbols = new DecimalFormatSymbols(Locale.getDefault());
applyPattern(pattern, false);
}
We can see here that even if we give a pattern, it uses the locale values. The javadoc also said :
Parameters:
pattern A non-localized pattern string.
To finish, we can go here to see the different local variant of numbers representation : http://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html
So we can see that US-English use the "dot format" and that Spanish use "comma format".
To answer your question : the proflem you're facing is probably due to the Decimal format of your locale. I advice you to be REALLY CAREFUL when converting types of objects to make manipulation on them. Converting an int to a String should be only to display it.
I think you should seperate decimal part of your number when it stills a float (or any decimal type) and then convert your object to a String to display it. You can take a look at Math class or search SO to get some example on how to this ;)
Also, as #Dmitry said, you can get DecimalSeparator with DecimalFormatSymbols.getDecimalSeparator().
Sources
Location.convert(double,int) source code
DecimalFormat(String) source code
Java "Decimal and thousands separators"
You are right, decimal seperator depends on your locale. You can get it by something like this
DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols formatSymbols = df.getDecimalFormatSymbols();
char separator = formatSymbols.getDecimalSeparator();

Getting invalid double, but the error shows a proper one

My application does some basic arithmetic processes and then adds them to a TextView. Because I want them the result be shown up to XX,XX I format my string with %.2f. Now, when I try to retrieve this result and use it in another arithmetic process, it gives me an error of:
java.lang.NumberFormatException: Invalid double: "8,86" (or any number for that matter)
How can I make the second process convert the string from the TextViewwithout getting an error?
process 1
int newProductQuantity = Integer.valueOf(productQuantity.getText().toString());
double newProductPrice = Double.valueOf(productPrice.getText().toString());
double newProductVAT = Double.valueOf(productVat.getText().toString());
double newProductPriceSum = ((newProductPrice + (newProductPrice * (newProductVAT / 100))) * newProductQuantity);
String newProductPriceSumTexta = String.format("%.2f", newProductPriceSum);
productPriceSum.setText(newProductPriceSumTexta);
process 2
double newOrderFinalLastSum = Double.parseDouble(newOrderFinalSum.getText().toString());
double newOrderFinalNewSum = Double.parseDouble(productPriceSum.getText().toString());
double newOrderFinalOmegaSum = newOrderFinalLastSum + newOrderFinalNewSum; //error is here
String newOrderFinalOmegaSumText = String.format("%.2f", newOrderFinalOmegaSum);
newOrderFinalSum.setText(newOrderFinalOmegaSumText);
your issue is Locale related. If you want always a dot . as separator, you should specify a Locale that use it. You can use format method that takes as first parameter a Locale object. For instance
String.format(Locale.UK,...
From the documentation of public static String format(Locale l, String format, Object... args)
Returns a formatted string using the specified locale, format string,
and arguments.
where
l - The locale to apply during formatting. If l is null then no
localization is applied.

change format of double like ***.***,**

I have double amount. Let amount to be 500000.12. I want to set value of the amount to TextView like this format 500,000.12 (where 12 is cents , 500,000 is dollars).
I wrote this function and it works
private String getAmountAsString(double amount) {
double integralPart = amount % 1;
int fractionalPart = (int) (amount - integralPart);
int integral = (int) integralPart * 100;
String strFractional = String.format("%,d", fractionalPart);
String strAmount = (strFractional + "." + String.valueOf(integral));
return strAmount;
}
But I think that there can be some easy and good way of doing this with java native functions. Can anybody help to find functions or some better way?
various Locale can be used to format float, double etc. You can use:
String.format(Locale.<Your Locale>, "%1$,.2f", myDouble);
Here .2f represents how many digits you want after decimal. If you are not specifying any locale it will use the default locale.
In String class this method is overloaded as:
format(String format, Object... args)
&
format(Locale l, String format, Object... args)
For more info have a look: Link1 , Link2 and Link3
Therefore NumberFormats are used. They are good to handle local differents for different countries.
//define a local, gets automated if a point or comma is correct in this Country.
NumberFormat anotherFormat = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
anotherDFormat.applyPattern("#.00");//set the number of digits afer the point
anotherDFormat.setGroupingUsed(true);// set grouping
anotherDFormat.setGroupingSize(3);//and size of grouping
double myDouble = 123456.78;
String numberWithSeparators = anotherDFormat.format(myDouble);//convert it
I think you have to take a look at this one
http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

Google Maps automatically convert period (.) into comma (,) from my Intent URI

I'm calling Google Maps Intent from my activity with this code found on StackOverflow:
final String uriContent = String.format(Locale.ENGLISH, "http://maps.google.com/maps?q=loc:%s", pCoordinate);
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriContent));
pContext.startActivity(intent);
where pCooriante contains entirely address such as 1.23456,7.8901.
It works well when my phone is using English as its language, but when I change it to French or Vietnamese (which use comma , as its number decimal seperator), it can't work anymore, because the query proceeded by Google Maps look like: 1,000,2,000 (it is shown in the search bar, and after that, a message like Cannot find 1,0000,2,0000 appears), although the exact URI I sent to the intent is 1.000,2.000 (the coordinate is converted to String to prevent Locale problems, and therefore the Locale.ENGLISH in String.format is more or less just abundant).
In short, Uri.parse(uriContent) return exactly the request with the query is 1.000,2.000, but Google Maps itself changed it. However, the code for direction works well with either Locale:
final String uriContent = String.format(Locale.ENGLISH, "google.navigation:q=%s", pCoordinate);
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriContent));
pContext.startActivity(intent);
Is there anyway to prevent the conversion of Google Maps? If I use geo:<coordinate>, it's fine, but I need a marker at that position.
Addional information:
This code final String uriContent = String.format("geo:0,0?q=%s&z=19", pCoordinate); doesn't work too, the periods are converted into commas.
This code final String uriContent = String.format("geo:%s?q=%s&z=19", pCoordinate, pCoordinate); can bring the map center to the coordinate, but still cannot put the marker there, and with the error "Cannot find 'coordinate with periods replaced by commas'"
I am using a temporary solution to this problem, by converting the decimal form of coordinates to degree one. (For example, instead of sending 10.768717,106.651488, I send 10° 46' 7.3812",106° 39' 5.3568"). The conversion is just simple mathematics operation.
However, there was a problem with Java float and double precision, and that was a lot of distance when sending to Google Maps. Therefore I change my input data, convert data using C#'s decimal and my Android app just use it without manupilating anything. Here is the convesion (C#) code:
protected String convertDecimalToDegree(decimal pDecimal)
{
int degree = (int)Math.Floor(pDecimal);
pDecimal -= degree;
pDecimal *= 60;
int minute = (int)Math.Floor(pDecimal);
pDecimal -= minute;
pDecimal *= 60;
return degree + "° " + minute + "\' " + pDecimal + "\"";
}
Usage:
String[] coordinates = shop.MapCoordination.Split(',');
decimal n1 = Decimal.Parse(coordinates[0]);
decimal n2 = Decimal.Parse(coordinates[1]);
shop.MapCoordination = this.convertDecimalToDegree(n1) + "," + this.convertDecimalToDegree(n2);
I will mark this as answer for now, but I appreciate any solution without having to convert to this form.
You can use the following snippet to solve the problem
public String getCoordinates(String coordinates){
if(Locale.FRANCE == Locale.getDefault()){
Pattern p = Pattern.compile(",.*?(,)");
Matcher m = p.matcher(coordinates);
if (m.find( )) {
int index = m.end(); //gets the second comma position
String str1 = coordinates.substring(0,index-1);
String str2 = coordinates.substring(index,coordinates.length());
NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE);
try {
str1 = nf.parse(str1).toString();
str2 = nf.parse(str2).toString();
} catch (ParseException e) {
e.printStackTrace();
}
return str1+","+str2;
}
}
return coordinates;
}
Updating the Google Maps app to the latest version (7.2.0) seems to fix the issue.
in Xamarin.Android:
using System.Globalization;
Android.Net.Uri.Parse("http://maps.google.com/maps?daddr=" + myLatitude.ToString(CultureInfo.InvariantCulture) + "," + myLongitude.ToString(CultureInfo.InvariantCulture)));

Categories

Resources