java: default number-formatting - java

I have a program that does algorithmic calculations with some number-output. I want this output to look nice and the program still being fast. I used the DecumalFormat but this makes it so slow, but works.
Is there a way to set the default number output so I wouldnt need DecimalFormat???
Locale deLocale = new Locale("de_DE");
// should be format: ###,###.### 123.456,789 de_DE
Locale.setDefault (deLocale);
double f=-123456.123458998;
System.out.println (""+f+""); // I wourld expect -123.456,123
// but the output is -123456.123458998
any ideas?? thanks!
chris

You need to look at the Customizing Format.
You need a ###,###.### - de_DE pattern.
String pattern= "###,###.###";
DecimalFormat myFormatter = new DecimalFormat(pattern);
double f=-123456.123458998;
String output = myFormatter.format(f);
System.out.println(f+ " " + pattern + " " + output);
EDIT : Use Predefined format, in case you don't want your own pattern.

Related

Use Android ICU NumberFormat with only the JVM

Android has the ICU library NumberFormat which allows formatting currencies in different ways, in my specific case I need it as ISOCURRENCYSTYLE. This works great but only works on Android.
Java as its own implementation of NumberFormat with an instance builder called getCurrencyInstance, which internally uses a default currency style and doesn't allow to specify it.
Some of the differences are:
BE_FR | Android: 123,12 EUR | Java: 123,12 €
BE_NL | Android: EUR 123,12 | Java: € 123,12
BE_EN | Android: EUR 123.12 | Java: €123,12
GE_GE | Android: EUR 123.12 | Java: €123.12
FR_FR | Android: 123,12 EUR | Java: 123,12 €
Is there a way to get the same Android output but only within the JVM, without using any external library?
A solution is to replace the prefix and the suffix of the DecimalFormat with the currency code:
import java.text.*;
import java.util.Locale;
public class Test
{
public static DecimalFormat geCurrencyFormat(Locale locale)
{
DecimalFormat df = (DecimalFormat)NumberFormat.getCurrencyInstance(locale);
String code = df.getCurrency().getCurrencyCode();
if(!df.getPositivePrefix().isEmpty())
{
df.setPositivePrefix(code + " ");
df.setNegativePrefix(code + " -");
}
if(!df.getPositiveSuffix().isEmpty())
{
df.setPositiveSuffix(" " + code);
df.setNegativeSuffix(" " + code);
}
return df;
}
public static void test(Locale locale)
{
DecimalFormat df = geCurrencyFormat(locale);
System.out.println(df.format(123.12));
}
public static void main(String args[])
{
test(new Locale("fr", "BE"));
test(new Locale("nl", "BE"));
test(new Locale("en", "GB"));
}
}
Output:
123,12 EUR
EUR 123,12
GBP 123.12
Unfortunately not. You can get the three-letter ISO 4217 codes using Currency and hand-craft things like:
Locale locale = Locale.FRANCE;
float amount = 123.1f;
NumberFormat nf = NumberFormat.getInstance(locale);
Currency currency = Currency.getInstance(locale);
String formattedAmount = nf.format(amount) + " " + currency.getCurrencyCode());
However, Android is using ICU code to correctly order the value and units in the currency string. This is not in the standard Java JDKs.
I understand you don't want to use a library, so this is out of scope of the required answer. However, should you choose to use a library, take a look at ICU4J https://unicode-org.github.io/icu/userguide/icu4j/. It includes a NumberFormat with ISOCURRENCYSTYLE matching the Android behaviour.

Issue with printing a degree symbol (°) in Swing

I have a simple program which displays data from a weather station in Swing. I am using System.out.print(temp + "\u00B0" + "F"); to display the temperature. On Windows this displays as "70°F", but on MacOS it shows "70�F". Is there a way to display the degree symbol reliably on all platforms?
Try this:
PrintStream out = new PrintStream( System.out, true, StandardCharsets.UTF_8 );
out.println( temp + "\u00B0" + "C" );
(by the way, I took the liberty of fixing your temperature measurement system too.)

Formatting output/Justifying field of width

I have to left-justify the station names and right-justify the number results in the example below for an assignment, ie Vegan Station needs to be left-justified in a field of width 15, but when I try it gives me an error for the %2d for the result. If anyone could help me understand how to correctly format it I'd really appreciate it.
System.out.printf("\n" + "\n" + "You rated each station as follows");
System.out.printf("\n" + "%-15s, Vegan Station" + "%2d", vegan);
System.out.printf("\n" + "Pasta Station " + "%2d" , pasta);
System.out.printf("\n" + "Waffle Station " + "%2s", waffle + "\n");
Here's the error:
java.util.MissingFormatArgumentException: Format specifier '%2d'
at java.util.Formatter.format(Formatter.java:2519)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)
at Survey.main(Survey.java:127)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
You're messing up your quotes. It should be:
System.out.printf("%n%-15s%2d", "Vegan Station", vegan);
Keep the format String together as a single String, and the variables to follow in a comma-delimited list.
Also for new lines, don't use \n but rather use %n when using printf.

Conversion from "datetime-local" to java.sql.Timestamp

I have a form with input of type "datetime-local" on a jsp page, the data is passed to a servlet:
String resetTimeString = request.getParameter(RequestParameterName.RESET_TIME);
How to convert the input to java.sql.Timestamp?
EDIT:
Well, I found something new!
You can use Timestamp.valueOf() to format a time-string with the value of yyyy-[m]m-[d]d hh:mm:ss[.f...]
So it can also handle micro/nano seconds. The only thing you need to do is replace the T with a space.
This works:
String datetimeLocal = "1985-04-12T23:20:50.52";
System.out.println(Timestamp.valueOf(datetimeLocal.replace("T"," ")));
The output:
1985-04-12 23:20:50.52
According to this site your resetTimeString looks like this: '1985-04-12T23:20:50.52' (a string)
I couldn't find a method to convert this to a timestamp directly, but you could just split it up manually:
String[] dateTime = datetimeLocal.split("T");
String[] date = dateTime[0].split("-");
String[] time = dateTime[1].split(":");
This will print:
System.out.println("DateTime: " + Arrays.toString(dateTime));
System.out.println("Date: " + Arrays.toString(date));
System.out.println("Time: " + Arrays.toString(time));
>>> DateTime: [1985-04-12, 23:20:50]
>>> Date: [1985, 04, 12]
>>> Time: [23, 20, 50]
After that you could just create a new Timestamp: (This is deprecated!)
Timestamp stamp = new Timestamp(Integer.valueOf(date[0]).intValue() - 1900,
Integer.valueOf(date[1]).intValue(),
Integer.valueOf(date[2]).intValue(),
Integer.valueOf(time[0]).intValue(),
Integer.valueOf(time[1]).intValue(),
Integer.valueOf(time[2].split("\\.")[0]).intValue(),
Integer.valueOf(time[2].split("\\.")[1]).intValue());
Note that, if you use this you need to subtract '1900' from the year and split dots with \\.
Also, you'd need to handle nanoseconds (In my example I'm using the value 50.52 as seconds, but the string returned from your server might not contain the nanoseconds)
You could also calculate a long from the date and use new Timestamp(<long>)
I hope this helps :)
Cyphrags' answer won't work if seconds are set to "00", because Chrome won't send the seconds part resulting in a java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff] when calling Timestamp.valueOf().
Therefore a more complete answer could be:
String datetimeLocal = "1985-04-12T23:20";
// make sure the seconds are set before parsing
if (StringUtils.countMatches(datetimelocal, ":") == 1) {
datetimelocal += ":00";
}
Timestamp value = Timestamp.valueOf(datetimeLocal.replace("T", " "));

how to log an integer value with log.d?

I am very new to Android and am trying some simple log to get a random background color. I have the code and it returns an integer between 1-256, or so I think. I need to log the value to check if it's OK, but I'm not sure how to log it with Android.. I've been using System.out.println("stuff") to log stuff in the past but I believe that's not how you're supposed to do it in Android.
I have my class:
public static int backgroundColorRandomize()
that returns
return randomRGB;
and I try to log it like this
Log.d(backgroundColorRandomize(), "value = " + randomRGB);
but I need to convert the returned value from backgroundColorRandomize to a String in order for it to log.
I tried java's .toString but I'm not sure I'm using it right.. Any help would be appreciated! Thanks!
Log.d("MYINT", "value: " + randomRGB);
private static final String TAG = YourClass.class.getSimpleName();
...
android.util.Log.d(TAG, String.format("value = %d. random color = %d", randomRGB, backgroundColorRandomize()));
More info:
http://developer.android.com/reference/android/util/Log.html
https://developer.android.com/tools/debugging/debugging-log.html
Logging libraries: https://android-arsenal.com/tag/57
I prefer String.valueOf(value).
Log.d(String.valueOf(backgroundColorRandomize()), "value = " + randomRGB);
Log.d(backgroundColorRandomize() + "" /* <-- all you need. */, "value = " + randomRGB);
I use
Log.d("MYINT", "value: " + randomRGB.toString());

Categories