BigDecimal myNumber = new BigDecimal(1234.56);
NumberFormat instance = NumberFormat.getInstance(Locale.GERMAN);
String localizedNumber = instance.format(myNumber);
System.out.print("\nformatting " + localizedNumber); o/p ->1.234,56
Till here code works fine but below line gives NumberFormatter exception as given string contains comma in it.
BigDecimal bigDecimal = new BigDecimal(localizedNumber);
I want numeric values to be localized but I cannot return string as putting number as string shows below error in excel
Number in this cell is formatter as text or preceded by an apostrophe
Is there any way by which I can return back numeric value(BigDecimal / Integer / BigInteger etc) in localized format
So that I won't get above error in excel and I can perform filter operations on excel data.
I've also tried new BigDecimalType().valueToString(value, locale); and new BigDecimalType().stringToValue(s, locale); of dynamic jasper reports api.
Happy to answer this question which asked me only and quite surprised that no one replied to this.
Actually we don't have to do anything for number localization in excel export because it's done by our operating system settings automatically.
Go to "Region and Language" -> Format -> Select language/country name -> Apply and check your excel earlier generated in English.
You will see numbers in currently selected country's number format. :)
Related
In Apache POI, I'm trying to read an Excel spreadsheet that has a Cell (raw) value of: 0.575
From what I understand, the purpose of the DataFormatter.formatCellValue() method is to return cell's value in the way it is shown in the Excel document.
The Cell format in Excel is Custom: "£"#,##0.00;[Red]-"£"#,##0.00
The way it displays in Excel is: £0.58
when I use:
DataFormatter formatter = new DataFormatter();
String displayValue = formatter.formatCellValue(cell);
I get: £0.57
Why is POI's formatter rounding down the value, when Excel rounds it up?
I've tried:
if( type == CellType.NUMERIC ) {
Format format = formatter.getDefaultFormat(cell);
if( format instanceof DecimalFormat ) {
DataFormatter.setExcelStyleRoundingMode((DecimalFormat)format);
}
}
But the Rounding mode doesn't get set, because the format is ExcelGeneralNumberFormat. One of the members of format is a DecimalFormat (as I can see from the debugger), but it seems I don't have API access to it.
I believe this is related: When I inspect the XML value of the cell, I see:
<xml-fragment r="D1" s="2">
<main:v>0.57499999999999996</main:v>
</xml-fragment>
Which is of course different to the "raw" value I see in Excel (0.575).
If the POI framework is rounding 0.57499999999999996 and not 0.575 then that of course explains why it's rounding down and not up.
The only way to reproduce the behaviour of Excel then, is to Round the raw value to 3dp (0.575) - then round again to 2dp (0.58)
I am reading a text file which has a field in Timestamp in this format "yyyy-MM-dd HH:mm:ss"
I want to be able to convert it to a field in Impala as BigInt and should like yyyMMddHHmmss in Java.
I am using Talend for the ETL but I get this error "schema's dbType not correct for this component"
and so I want to have the right transformation in my tImpalaOutput component
One obvious option is to read the date in as a string, format it to the output you want and then convert it to a long before sending it to Impala.
To do this you would start by using Talend's parseDate function with something like:
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",row1.date)
This parses the date string into a Date type object. From here you can convert this into your desired string format with:
TalendDate.formatDate("yyyMMddHHmmss",row2.date)
Alternatively this can be done in one go with:
TalendDate.formatDate("yyyMMddHHmmss",TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",row1.date))
After this you should have a date string in your desired format. You can then cast this to a Long using a tConvertType component or the following Java code:
Long.valueOf(row3.date)
Or, once again we can do the whole thing in a one liner:
Long.valueOf(TalendDate.formatDate("yyyMMddHHmmss",TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",row1.date)))
From here you should be able to send this to Impala as a Java Long to an Impala BIGINT field.
I have a string like "2,345".I want to put it into a excel cell.I successfully did but in my excel file i got "2,345" as a string.So please suggest me how can i get "2,345" as a number value but with the same format as i used above(comma seperated).
Thanks in advance.
Remove the comma before inserting it into Excel, cast it to a number before inserting, then format the column to show the comma.
String replace
In Excel the code to format a Range with commas is:
SomeRange.Style = "Comma" 'or, recorded version
SomeRange.NumberFormat = "_-* #,##0_-;-* #,##0_-;_-* ""-""??_-;_-#_-"
'a simpler version..
SomeRange.NumberFormat = "#,##0"
I have made an app that takes some values and adds them to a txt file.
It does something like this,they are strings[] :
product[1] quantity[1] price[1]
product[2] quantity[2] price[2]
.....
product[n] quantity[n] price[n]
The problem is,most of the time product[1] won't have the same lenght as product[2] or the other products and the same goes for quantities and prices.This results in a messy text layout,something like this.
ww 2 4
wwww 1 2.5
w 1.2 1.1
Is there any way i can make it tidier ? Something like creating a table or columns?
Thanks !
EDIT : To make it a bit clearer,i want to find a way for the stuff in the txt file to be arranged like this,instead of how it is in the above example
ww 2 4
wwww 1 2.5
w 1.2 1.1
At the moment i'm using this
pw.println(prod[n]+" "+cant[n]+" "+pret[n]);}
But this is making the text in the txt file be unaligned(example 1)
Use the format Method of the String class like this:
Declare a String with the format
String yourFormat = "%-10s %-10s %-10s%n"; //choose optimal ranges.
//if you exceed them, it will always automatically make one space
//between the next column
write the output with that format:
output.write(String.format(yourFormat, firstString, secondString, thirdString));
first string are your w's, second and third are the columns with numbers.
for your example:
String myFormat = "%-10s %-10s %-10s%n";
for(int i=0;i<prod.length();i++){
pw.println(String.format(myFormat, prod[n], cant[n], pret[n]));
}
more info here and here
How to get TimeZone value in the Java 1.6 through OsloneTimeZone Database.
I am passing base64 string of timeZone lyk. "Asia/Kolkata". After that I have to get this Tiezone value in numerical format like + 5.5.
How to do this please suggest.
Fairly simple. This timezone database is built into java 1.6, however you might need to update your version of tzdata, please check here.
So the code then is as follows
TimeZone localTimeZone = TimeZone.getTimeZone("Asia/Kolkata");
// next line yields milliseconds
int rawOffset = localTimeZome.getRawOffset();
// next line converts to hours
double hourOffset = (rawOffset / 1000) / 3600.0;
If you are happy with a numeric representation (no leading plus if the value is positive) then you are done. If you want a version with a leading plus, add the following line:
String hourOffsetString = (hourOffset > 0.0 ? "+" + hourOffset : "" + hourOffset);
Running the full code for me yields +5.5 which I think we both agree is the correct offset for Kolkata.