i'm learn Java thourgh Android Studio with Java, i was stuck at the Float Format, how can you display, for example: 1.234E10 instead of 12340000000000, thanks guys
12340000000000 is actually 1.234E13
DecimalFormat formatter = new DecimalFormat("0.###E0");
String format = formatter.format(12340000000000.0);
System.out.println(format);
Output:
1.234E13
1.234E10 is actually the same as 12340000000000 only it's printed out in an engineering format and it's a hell of a lot more readable than 12340000000000.
Related
I have a problem with parsing decimal number in Linux environment. When I parse in Windows, everything's all right.
Below, code snippet
String price;
DecimalFormat FORMATTER = (DecimalFormat)DecimalFormat.getInstance();
double customPrice = FORMATTER.parse(price).doubleValue();
And results
When price='9' Then customPrice='9.0' - it is ok
When price='1,00' Then customPrice='100.0' - it is wrong
When price='25,00' Then customPrice='2500.0' - it is wrong
Can you tell me what the problem is ?
Thanks
Read here here about locales, probably you're using en_US which causes the ',' symbol to separate groups of thousands.
you can use also
DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);
unusualSymbols.setDecimalSeparator(',');
DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
weirdFormatter.setGroupingSize(4);
to state your own sepeartors
You can change the decimal separator of DecimalFormat. The easiest way by using a NumberFormat with the desired locale, e.g.:
NumberFormat FORMATTER = NumberFormat.getNumberInstance(Locale.GERMAN);
double customPrice = FORMATTER.parse(price).doubleValue();
I would like to ask how can i get string with 'now' date in the following format?
"2015-10-08T08:09:22.067+00:00" ?
Many thanks for any advice.
Have a look here at the simpledateformat method:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
I think you should use the simple date format to format your date string . For this, consider reading the this link .
But if you want just to change the string and get the time then just follow this link or this link
I hope this answer would help you.
I'm trying to format a date for a given locale new Locale("mk", "MK"). The locale is valid, it returns the country name and language properly. I want to use custom string, in my case "E, kk:mm" or "EEEE, kk:mm". I want the output to be "сабота, 12:00", but what I get is "7, 12:00".
This is how I use it and I tried many ways, but they all seem to behave the same.
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, kk:mm", new Locale("mk", "MK));
sdf.format(new Date());
// output: 7, 12:30
Another method I tried
Calendar calendar = Calendar.getInstance(new Locale("mk", "MK"));
calendar.setTimeInMillis(new Date().getTime());
DateFormat.format("EEEE, kk:mm", calendar);
// output: Saturday, 12:30
I also tried using java.text.DateFormat instead android class, but no change.
The phone locale is set to English, but this is localized app, I want to show dates in a fixed locale format.
I've looked into many SO question regarding this issue and I wasn't able to find answer. I'm not interested in predefined formats, I want to use my own format and I want the date/month names to be formatted for the input locale.
I think the problem is that Macedonia is not a supported locale on the Android JVM. If you run your code as plain Java console app, it's fine. The method Locale.getAvailableLocales() returns 152 members in plain Java, only 88 in an Android emulator. If you have the code snippet:
Locale[] locales = Locale.getAvailableLocales();
String cCode;
for (Locale loc :locales){
cCode = loc.getCountry();
if (cCode.equalsIgnoreCase("MK"))
Toast.makeText(this, cCode, Toast.LENGTH_SHORT).show();
// Or System.out.println() in a Java app
}
Then the toast doesn't show for "MK" although it will println in the Java app
From documentation of SimpleDateFormat:
**Text**: For formatting, if the number of pattern letters is 4 or more,
the full form is used; otherwise a short or abbreviated form is used if
available. For parsing, both forms are accepted, independent of the
number of pattern letters.
So this should fix it:
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, kk:mm", new Locale("mk", "MK"));
NickT was faster :-), so just adding to his answer: if you want to see your locales supported on Android, run:
for (Locale l:Locale.getAvailableLocales()) {
Log.d(l.getDisplayCountry(),l.toString());
}
and you will see that Macedonia is not on the list.
How do I format time using Android's Time Class.
I want to display time in this format "hh:mm AM/PM" . I have tried using the Time.format function but I'm not sure if I'm using it correctly.
Thanks
Please try this..
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm aa");
Date dt = new Date();
String strValue = timeFormat.format(dt);
Time time = new Time();
time.set(0, 0, 17, 4, 5, 1999);
Log.i("Time", time.format("%d.%m.%Y %H:%M:%S"));
try:
String strTime = time.format("%I:%M %p");
Your answer can be derived from, but not limited to this link, a C++ reference about "ctime":
http://www.cplusplus.com/reference/ctime/strftime/
I found this link to be very helpful deciphering the string formats used in my own work.
The Android.Text.Format.Time.Format docs assumes you know something:
Where or how to read "man" page for strftime, which if you weren't familiar using Linux (or a Mac at the terminal) might require some creative web searching or know what "See man strftime for what means what." referred to. Informal as it is in official documentation, it does build off and reference what has already has come before and is left as an exercise for the developer.
How do I get these formats in java?
Input:
1223893
180703
80967
1461
700
Output :
1,223,893
180,703
80,967
1,461
700
I will be always converting one by one number, this was just to get more examples.
you can read up on java number formatting here
so you would do something like this:
DecimalFormat myFormatter = new DecimalFormat('###,###,###');
String output = myFormatter.format('1223893');
if you output the output var it should have 1,223,893
Look for "grouping" and "thousands separator" here. DecimalFormatSymbols provides setGroupingSeparator(',') and you can set it on a DecimalFormat, together with setGroupingSize(3). To illustrate:
DecimalFormat df = new DecimalFormat();
df.getDecimalFormatSymbols().setGroupingSeparator(',');
df.setGroupingSize(3);
System.out.println(df.format(1223893)); // prints 1,223,893
You could use DecimalFormat.
Take a look at the DecimalFormat class.
google for NumberFormat in java
See the api docs.