I have a bean which has a property of type double. When I pass this bean to view layer, it shows the value in scientific notation For example: 9.78313E+10 instead of 97831300000.
I want the result without scientific notation and I cannot change the data type of that field. Please let me know if there is any workaround.
You can use the printf() with %f:
System.out.printf("%.0f", value);
Here you can find a beautiful printf format cheat sheet by Alvin Alexander that might help you (and hopefully others) a lot.
How the number is shown depends on the formatting that is used by the component showing the value. If you can't access that code to use a different formater; then there is nothing you can do.
See https://docs.oracle.com/javase/tutorial/java/data/numberformat.html for starters.
One option might be: can you exchange the bean implementation; so instead of providing a "double" number; can you change it to provide a string (in that case, you could control the formatting of the number).
Related
I have a big number in a database; in this case, 10,000,000,000. Whenever I use that information for something, like sending a message with it, instead of 10,000,000,000, it says 1E10, and I really do not want that.
Can I avoid that in any way?
If I go to the database, the value is 10,000,000,000.
It's the same number, just represented in scientific notation.
Since you don't describe how you are storing the value, you can use DecimalFormat#getNumberInstance to help format it to one that doesn't contain the scientific notation.
double foo = 10000000000L;
System.out.println(foo);
System.out.println(DecimalFormat.getIntegerInstance().format(foo));
This outputs:
1.0E10
10,000,000,000
i don't know what the integer can i use it in this function
so i have a problem to show arabic in my creating pdf
i use iText library to get this function
if some on know how to use it please inform me
You shouldn't use int values such as 0, 1, 2,... in your code as it will be very hard for people to know what these values mean (just like you currently have no idea which options are available).
Instead you should use constants that are provided by iText. The API documentation informs you that the parameters for the setArabicOptions() method can be a combination of:
ColumnText.AR_NOVOWEL: Eliminate the arabic vowels,
ColumnText.AR_COMPOSEDTASHKEEL: Compose the tashkeel in the ligatures, or
ColumnText.AR_LIG: Do some extra double ligatures.
If you want to know which exact int values correspond with these constants, you can always print them out or look inside the code, but there is no reason to do this.
The different values are actually to be used as flags (or bits). You can combine these values like this:
column.setArabicOptions(
ColumnText.AR_NOVOWEL |
ColumnText.AR_COMPOSEDTASHKEEL |
ColumnText.AR_LIG);
When debugging in intellij, I can tell it to display a given int variable with its decimal value or its hex value.
Is it possible for the debugger to display BOTH at the same time?
No, it's not possible.
When debugging, you can right click on an integer variable and choose View As > and either Hex or Primitive, not both.
This setting only applies to the single variable and doesn't seem to persist through different debugging sessions.
I had a look at the possibility of using a Data Type Renderer (right click a variable and go in to Customize Data Views... to see the dialog).
A Data Type Renderer only applies to complex data types (not primitives). So if for example your int that you wanted to see 38 and 0x26 was always part of a complex object (e.g. Person.age) then perhaps a data type renderer could help.
Alternatively you could add a String watch expression that constructs a String value for both the decimal and hex value of your int.
Sorry that there is not a way to do exactly what you want but I hope these 2 other suggestions are useful to consider.
Yes it's possible, at least in Android Studio 2.1 which is built on intellij.
Open Settings -> Built... -> Debugger -> Data views -> Java and enable Show hex value for primitives. Then it shows integer and hex in braces in debugger.
Starting from IDEA 2016.3 it's possible to define custom Java Type Renderers for primitive types (including arrays): https://www.jetbrains.com/idea/whatsnew/#v2016-3
With this feature you can display your primitive value any way you like.
I am using freemarker and trying to display numbers in this format: $3,343,434.00 for example. This was easily taken care of by using ${total?string.currency} (assuming "total" is some number).
However, when I have negative numbers, it's showing them like this: ($343.34) instead of this: -$343.34. I need the negative sign instead of the parenthesis. Is there a way I could customize the formatting so it does everything that the string.currency did but replace the negative value behavior? I am relatively new to freemarker, so detailed responses are appreciated!
You can also try ?string(",##0.00"). However in this case you need to explicitly add $ and - sign would be after $ in case of negative numbers.
<#local total = 3343434/>
$ ${total?string(",##0.00")} //$ 3,343,434.00
<#local total = -3343434/>
$ ${total?string(",##0.00")} //$ -3,343,434.00
OR in case if you want what was expected you can replace the strings.
<#local total = -3343434/>
<#local total = "$ " + total?string(",##0.00")/>
${total?replace('$ -','- $')} //- $3,343,434.00
Update: Since FreeMarker 2.3.24 you can define named custom number formats, which can be an alias to a number format pattern (or even a formatter implemented in Java, but that level of flexibility isn't needed in this case). So add a custom number format called "money" as an alias to "ยค,##0.00" to the FreeMarker configuration, and then you can write something like ${total?string.#money}. See: http://freemarker.org/docs/pgui_config_custom_formats.html
Currently FreeMarker just uses the formatting facility of the Java platform, so it's only as configurable as that (assuming you want to use ?string and ?string.somethingPredefiendHere). Which is not much... but, in general, the formatting categories provided by the Java platform is not fine-gradient enough anyway, I mean, you don't have application-domain categories like, price-of-product, a salary, a price on the stock, etc. (This demand is more frequent with non-currency numbers though.) So I think, generally, you want to make a formatter function, that you can use like ${salary(someNumber)}, ${price(someNumber)}, etc. Those functions can be implemented in a commonly #included/#imported template like a #function or in Java by using #assign salary = 'com.example.SalarayMethod'?new() in place of #function, where com.example.SalarayMethod is a TemplateMethodModelEx.
How about taking a mod of your number, convert it to the required string format and finally add a '-' prefix to the final string. You can retain the default format in just two steps.
Freemarker uses the currency formatting provided by the Java platform.
It requires a little tweaking of the DecimalFormat returned by NumberFormat.getCurrencyInstance() (which is what is called when you call .currency). You can see examples of it here.
However, that said it will likely be more effective for you to create a macro in freemarker to call which will handle your specific formatting.
Sorry for not having an example of what that macro would look like, but it's a good starter into macros in freemarker since you are just learning.
You might investigate if you can supply a custom format using the exposed configuration for number formats that will meet your needs.
If you want to maintain the default currency formatting (in case you need to use a locale other than '$'), you can just replace the parentheses like so:
${transaction.amount?string.currency?replace("(","-")?replace(")","")}
This will work without error regardless of if a number is negative or positive.
TIP: Make sure the number is actually a number with the ?number directive before converting to a currency format
I just bumped into this little problem and I wanted the input of other people on this
I was wandering what was the best solution to convert a String to an int
(int)(float)Float.valueOf(s)
or
Float.valueOf(s).toInt()
s is a String inputed from a textfield so I can not guarantee that it is necessarily an int
my instincts is that the double cast is ugly and should be avoided
Your input?
Your requirements are unclear:
If you are expecting an integer and don't want to allow the user to enter a number with a decimal point in it, simply use Integer.valueOf(String) or Integer.parseInt(String) and catch the NumberFormatException.
If you want to allow numbers with decimal points, then use Float.valueOf(String) or Float.parseFloat(String).
If you simply want to truncate the float to an int then either Float.intValue() or two casts are equivalent. (The javadoc for intValue explicitly states this.)
If you want to round to the nearest int, use Math.round() instead of a cast.
You should catch NumberFormatException whatever approach you take, since the user could enter rubbish that is not a valid base-10 number (with or without a decimal point) ... or that exceeds the bounds of the number type.
(I suppose that you could use a regex or something to check the String before trying to convert it, but it is simpler to just let the exception happen and deal with it. The exception efficiency issue is unlikely to be a practical concern in this use-case.)
On your original question as to whether intValue() is better than two casts: it is a matter of style. The two approaches do the same thing ... according to the javadoc. It is possible that one will be slightly more efficient than the other, but:
that shouldn't be a concern for your use-case, and
the only way to know for sure would be to profile the two alternatives on your execution platform ... and frankly it is not worth the effort.
You should use Integer.valueOf(..), and catch the NumberFormatException (if the string cannot be parsed as an integer).
Integer.parseInt(string) would be best
int x = Integer.parseInt(s);
Would be best to check if the string is an int before calling this.
As others pointed out, its just matter of style not performance... but if you are worried about performance you should validate the text field data in browser itself by javascript using a regex
^[0-9]*$
which would allow only integers to be submitted to your back-end code and hence improving performance by avoiding one network trip. Still you should validate the data in back-end, for that you can use
Integer.parseInt(String s) throws NumberFormatException