convert a double scientific notation to decimal notation in JNI - android

I have jdouble(double) value coming in from my activity which when printed gives in scientific notation i.e in E format. Now i want to show it in decimal format and truncate it so i used "%.2f" in my format specifier to do that. But weirdly the "%f" format specifier is showing 0.000000 as the final value. please suggest some advice.
P.S I'm doing this on the native side of android.

In order to show decimals instead of E format notation and truncate a number you can use NumberFormat:
NumberFormat formatter = new DecimalFormat("#0.000");
An then, where you want to use your number:
formatter.format(yournumber)
e.g.:
Toast.makeText(context, "Truncated number: " + formatter.format(yournumber), Toast.LENGTH_LONG).show();
In this case, your number will be shown with 3 decimals.
Add as many zeros as decimals you want to show.

Related

Converting Numbers to Local (UTF8) Bengali numbers

I am trying to convert the English numbers (1, 2, 3) to Bengali numbers (১, ২, ৩).
For example, if I get 10000, then I want to show like ১০,০০০.
I can replace the number one by one with the Bengali counterpart using replaceAll method
But I want to know if there is an alternative solution to do that instead of the above.
Use this:
val convertedString = String.format(Locale.forLanguageTag("bn"), "%d", 1234567890)
I have used NumberFormat from popular library Intl and converted it easily like below
NumberFormat("##,##,##,###", "bn").format(10000)
And the output is:
১০,০০০

Type conversion for multilanguage java

My app supports multi language, i have a problem when dealing with numericals. Suppose my selected language is hindi and I perform some calculation operation on the value taken from edit text. I had to format the float value after the calculation (say like my resulting calculation gives me 12.345678) i will use the decimal format class to get only 12.34 (like only two digits after decimal point). Till here is fine, but i wanted the resulting string from decimalformat class( i.e: 12.34) to be back as a float. Here the crash happens stating NumberFormatException. Because the resulting 12.34 comes out as a hindi 12.34
How can i format the resulting calcution (i.e: 12.3456) to 12.34 without converting it to string(i.e: decimalFormat.format(value)).

Decimal Format separator

I have a problem with formatting decimal values for localizing in Spanish (and other European languages, as well) when the decimal separator for Spain comes out as a comma ,.
How can I get the decimal separator as dot .?
Here is the code I used for formatting a decimal number
DecimalFormat twoDecimalForm = null;
twoDecimalForm = new DecimalFormat("#.##");
DecimalFormatSymbols.getInstance().setDecimalSeparator('.');
String valueInDecimal = twoDecimalForm.format(0.005 * progressValue);
//Exception here
Log.d(TAG,"valueInDecimal" + Double.valueOf(valueInDecimal));
I am getting a comma , instead of of a dot .
A workaround can be replace the comma with ..
But is there any other solution?
If you getting data from web then ask your web developer to pass data as per your requirement, then your problem gets resolved or you can go with replace option.

How to return 1 decimal place for the answer of this calculated Label in Adobe Flash Builder

I am writing code in Adobe Flash Builder for an Android application. I have written my code to do some math and return the answer to a label field. I would like to know how do I return this answer to show only 1 spot after the decimal. Here is the code
lblAnswer.text = String(Number ((sldrABSL.value) + 46.7)/28.7);
If there are any suggestions please let me know.
If I understand this correctly, you have a string representing a number, which you want to be presented with only one decimal.
Fist of all, you'd have to convert the numeric value of the string to a double:
String stringOfNumber = "100.1233123";
Double number = Double.valueOf(stringOfNumber);
Secondly, you'd have to establish the format of which to represent the double (number of decimals):
DecimalFormat oneDigit = new DecimalFormat("#,##0.0");
Set the digit to a (i.e) TextView:
myTextView.setText("" + oneDigit.format(number));
I think this should work. Is this kind of what you were asking?
Edit: Not super certain as to how to set it to a textview, but in java, printing it to screen works like this:
System.out.println(oneDigit.format(number));
Edit2: Oh, and you'll need to this import:
import java.text.DecimalFormat;
Edit3:
TextView.setText("" + oneDigit.format(number));
works fine for me.
The decimal places uses the tofixed property. It must be added at the end of the specified number that needs to contain the decimal place.
'lblAnswer.text = String(Number ((sldrABSL.value) + 46.7)/28.7).toFixed(1);
The one specifies the number of decimal places that are used.

String.format uses comma instead of point

My app is working on many devices without problems so far. But now I got my new Galaxy Tab with Android 3.2 where it crashes all the time. I found out that the problem was a float in an EditText.
I am using myEditText.setText(String.format("%.1f", fMyFloat)); to put the float in the EditText. But somehow the float on my 3.2 Galaxy Tab is generated with a comma instead of a point. When I read the EditText back the app crashes of course, telling me that this is no valid float because of the comma...
What is going wrong here?
Convert float to string..
From the documentation of String.format:
String.format(String format, Object... args)
Returns a localized formatted string, using the supplied format and arguments, using the user's default locale.
The quoted text above means that the output of String.format will match the default locale the user uses.
As an example a comma would be used as the decimal-point-delimiter if it's a user using Swedish locale, but a dot if it's using an American.
If you'd like to force what locale is going to be used, use the overload of String.format that accepts three parameters:
String.format (Locale locale, String format, Object... args)
Convert string to float..
Parsing an arbitrary string into a float using the default locale is quite easy, all you need to do is to use DecimalFormat.parse.
Then use .parse to get a Number and call floatValue on this returned object.
Your format call on your Galaxy Tab uses some default Locale which in turn uses , for floats. You could use String.format(Locale,String,...) version with specific locale to make things work.
Or you should've used same locale both for parsing and formatting the number. So you should probably go with NumberFormat to format and parse your floats.
String.format uses the locale you are in. You should do something like this if you want a dot:
NumberFormat formatter = NumberFormat.getInstance(Locale.US);
myEditText.setText(formatter.format(fMyFloat);
Have a look into NumberFormat for more formatting options
Use below code it's works for me:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern(pattern);
String output = df.format(value);
System.out.println(pattern + " " + output + " " + loc.toString());
Summing up previous answers, an easy way to have the dot instead of the comma in all country, is this:
myEditText.setText(Locale.CANADA, String.format("%.1f", fMyFloat));
And you will have your String formatted with the dot

Categories

Resources