Having trouble format decimal point in Excel with JasperReport - excel

I made an excel report, which contains some Double type TextFields with number format pattern ##0.#
In OpenOffice, these cells are formatted correctly (Which is exactly what I need)
1.56 -> 1.6
0.0 -> 0
However, when I opened report with Microsoft Excel, the decimal point did not disappear like I excpected.
1.56 -> 1.6
0.0 -> 0.
After some search, I found a post about number format in Excel.
In Excel format number with optional decimal places
I tried [=0]0;.# instead as the post suggested. But Excel complain about about my cell number format are broken.
My question is : What pattern should I use in JasperReport, so Excel won't show the trailing decimal point ?
UPDATE : I just find out the pattern described in link dose not solve all cases, it match only 0, not 1.0, 2.0... etc.

You can try to use net.sf.jasperreports.export.xls.formula property.
The sample:
<textField pattern="##0.#">
<reportElement x="0" y="0" width="100" height="20">
<property name="net.sf.jasperreports.export.xls.formula" value="[=0]0;##0,#"/>
</reportElement>
<textElement/>
<textFieldExpression><![CDATA[$F{sum}]]></textFieldExpression>
</textField>
In my case the comma (,) is delimiter in my OS.
The information about net.sf.jasperreports.export.xls.formula property you can find here.

As workaround you may paste in field this code:
new DecimalFormat("0.##").format($F{bigdecValue})
So, processing for xls will be without comma if thera are trailinig zeros.

Related

Truncate decimals without rounding in NetSuite advance html pdf templates [duplicate]

I am using Freemarker template,I have a decimal value:
a = 23.65
I just want to retrieve above value as:
a = 23.6 <#-- Extract first number after decimal point -->
I have used number_format ex :
<#setting number_format="0.#">
But it rounded off the value after decimal point to 23.7.
Could anybody know how to extract first number after decimal point without rounding off?
You need to use extended formatting option in order to specify a roundingMode.
Please note, that you need at least FreeMarker 2.3.24 for these to work.
By default freemarker uses halfEven rounding mode. For your case, you can try to specify down
${(23.65)?string(",##0.0;; roundingMode=down")}
You can check this expression online here.

Text to columns: How to prevent date formatting of decimal numers?

With a dataset such as this:
Date, Value
04.03.2020, 13.35
04.03.2020, 13.8
04.03.2020, 21.21
You can split the data using Data > Text to columns and specifying the value separator.
The problem is that Excel recognizes 13.8 as august 13, so the output is:
Date Value
04.03.2020 13.35
04.03.2020 13.aug
04.03.2020 21.21
How can I make sure that Excel never interprets decimal numbers such as 13.8 as dates?
I'm working in a region that uses , as decimal separator, but I often have to work with data that is set up with . as the decimal separator. One work-around is of course to replace , with ; and . with , before opening a .csv file. And if the only other solution is to set it up with VBA, I'm perfectly able to do so myself. But I often find myself trying to help colleagues on their computers without my own VBA configurations ready to go. So if there's any other way to do this using standard Excel system settings, that would be great!
This little problem has bugged me for years and I'm eager to get rid of it once and for all.
Edit:
I'm running Excel version 1908 on Windows 7, Office 365.
This problem often occurs when I'd like to inspect csv files that are recognized as Excel files on my system. There have been suggestions to format the cells as General before splitting the data. That does not seem to work on my end.
Another solution is to get what you want with formulas:
Formula for Dates:
=TEXT(LEFT(A2,FIND(",",A2,1)-1),"General")
Formula for Values:
=TEXT(MID(A2,(FIND(",",A2,1)+2),(LEN(A2)-(FIND(",",A2,1)+1))),"General")
Results:
During Text to Column process at Step 3 you have the option to to select Data Column Format you could also select the Value column and play around with Text or General formating.

Why do Excel values in parentheses become negative values?

A colleague and I encountered a behavior in Excel which isn't clear to us.
Background:
We have a tool which converts an Excel sheet into a table format. The tool calculates the formulas which are in excel and replaces variables inside it with specific values.
The excel tool is used by one of our customers who use values like (8) or (247).
These Value are automatically translated by excel to -8 or -247.
Question:
I saw that many people want to display negative numbers in parentheses. But why would Excel change values in parentheses to a negative number?
I know that I could simply change the cell config to text and this would solve the problem but I wonder if there is a reason for the behavior, since there seems to be no mathematical reason for this.
Its simply the different format of cells you are bringing the "values from" and "pasting to". ..... numbers with parentheses are in cells with "accounting" format and negatives are stored in general or standard number formated cells. To resolve you can change the format of destination cells to accounting using cell formatting as number>accounting.
To answer the why, it's because accountants put negative numbers in brackets for readability
Unfortunately, this is one of the excel feature/bugs that helps some folks and frustrates others. When opening a file or pasting content, excel will immediately and always try to parse any values into formats it deems appropriate, which can mess up data like:
Zip Codes / Tel. # → Numeric: 05401 → 5401
Fractions → Dates: 11/20 → Nov, 20th YYYY
Std. Errors → Negative Numbers: (0.1) → -0.1
For some workarounds , see Stop Excel from automatically converting certain text values to dates
Once the file is open/pasted, the damage is already done. At that point, your best bet is:
Updating the field and displaying as text (appending with ') to prevent re-casting
Formatting the field if the operation wasn't lossy and is just presenting the info differently
Running a clean if/else to pad or other convert your data based on the identified errors
Specific to displaying values back in parens, if excel is converting them and treating them like negative numbers (which may or may not be the appropriate way to actually store the data), you can apply a different format to positive and negative numbers to wrap back in parens.
It is standard practice to write negative values as numbers in parentheses, especially in accounting. This makes negative values stand out much more than a simple negative hyphen; compare -1 and (1).
Excel is a tool very commonly used by accountants and supports accountant-style spreadsheets. Therefore, entering (100) means having a value of -100, even if there is no minus hyphen!
Here is a fun fact, if you enter (-10), Excel will treat it as normal text.

Specific number FORMAT mask like #,###,##0.0### that would force Excel GENERAL NUMBER format?

What custom format mask will display 3.0 as 3 and 3.10 as 3.1?
I am trying to support a Cognos report that uses custom SQL that selects from an Oracle column that is defined as NUMBER(11, 4). if I do not apply any formatting to the.1 column on the report, the resulting Excel column has "Number " category formatting applied with 4 decimals.
As a result, I see the following sample formatted values displayed:
a) 4,123.3400
b) 4.0123
c) 4.0100
d) 3
e) 0.10000
I would prefer to see only significant zeros afer the decimal point displayed. For example, 4.0100 should be 4.01 but otherwise the formatting is fine. When the number is a whole number, I prefer not to display a decimal point at all. For example, 3 is preferable to 3.0 for values with no fractional part.
I tried experimenting with custom formats and the closest that I can come to what I am looking or is this format
#,###,##0.0###
which produces:
a) 4,123.34
b) 4.0123
c) 4.01
d) 3.0
e) 0.1
There are two differences between using no format, the first example and my custom format:
Whole numbers are displayed with one zero after the decicimal (d)
Trailing insignificant zeros after the decimal are suppressed. (a)
What I woold like to see is the following, the difference with the opreceding example is that whole numbers are displayed without any decimal (d):
a) 4,123.34
b) 4.0123
c) 4.01
d) 3
e) 0.1
Excel acheives this by default with its "General" format, however, the trick is trying to get Cognos to produce that setting using custom SQL that selects from a NUMBER(11, 4) column. As mentioned above, going with the default format results in Excel output that uses the General format but has 4 decimal places, so I get 4 decimals of precision if there is any faction part. The csolution appears to be to use a custom format.
Can my format
#,###,##0.0###
be tweeked to work like Excel's General format for numbers?
The answer is no, you can't get all the formatting you want with one single format.
You should know that even Excel's General does not get everything you want, because that doesn't provide thousands separators.
You'll have to pick something to give up. If you can give up the commas, then why not just use General?
Also note that you have superfluous # marks. Your formatting is equivalent to #,##0.0###.
See this related question for potential further ideas.

Can someone confirm how Microsoft Excel 2007 internally represents numbers?

I know the IEEE 754 floating point standard by heart as I had to learn it for an exam. I know exactly how floating point numbers are used and the problems that they can have. I can manually do any operation on the binary representation of floating point numbers.
However, I have not found a single source which unambiguously states that excel uses 64 bit floating point numbers to internally represent every single cell "type" in excel except for text. I have no idea whether some of the types use signed or unsigned integers and some use 64 bit floating point.
I have found literally trillions of articles which 1) describe floating point numbers and then 2) talk about being careful with excel because of floating point numbers. I have not found a single statement saying "all types are 64 bit floating point numbers except text". I have not found a single statement which says "changing the type of a cell only changes its visual representation and not its internal representation, unless you change the type from text to some other type which is not text or you change some other type which is not text to text".
This is literally all I want to know, and it's so simple and axiomatic that I am amazed that I can find trillions of articles and pages which talk around these statements but do not state them directly.
Excel 2007 supports the OpenXML format which is a ZIP file (.XLSX) containing a bunch of XML files. There is an SDK to work with the OpenXML format you can get the docs for it here, and download it here.
Basically numbers are stored as plain text within a element so if cell A1 has the number 42 and cell A2 has the number 81.56 in the UI, the XML would look like the follow XML fragment:
<row r="1" spans="1:2">
<c r="A1">
<v>42</v>
</c>
<c r="B1">
<v>81.569999999999993</v>
</c>
</row>
When working with OpenXML I would highly recommend just using the SDK and not going after it on your own.
This page has a reference for internal Excel data types.
It stored the numbers as double.

Resources