I would appreciate an answer how to solve the following problem.
I use CarlosAg.ExcelXmlWriter and I have a problem with formatting decimal numbers in Excel.
Example: I have SQL field TestField, Decimal(15,2)) with value 123.45
When i transfer the result (DataSet and some C# routine) into Excel with CarlosAg.ExcelXmlWriter the result is always 12345 - as though Carlos does not recognizes decimal number correctly.
I tried formatting data on SQL side and on Carlos side and result is always concatenated number (123.45 -> 12345)
A a side note - i did sort of "solve" the problem by casting the number into varchar but that is not acceptable for our customer because he can not format strings in the excel (like making SUM etc..) and he wants the Excel field to be explicitly numeric not string.
I am almost shure there is some simple answer for that.
Thanks
Damir
I tried Carlos with NumberFormat :
//style2.NumberFormat = "Scientific";
//style2.NumberFormat = "General Number";
//style2.NumberFormat = "###,###.00";
//style2.NumberFormat = "###.###,00";
...
case "System.Decimal":
case "System.Double":
wc = new WorksheetCell(dtrrow[col.ColumnName].**ToString().Replace(",",".")**, DataType.Number, style_number);
row.Cells.Add(wc);
...
Related
I have a Excel file with two columns. One has a name other has the corresponding mass to it. I have used the corresponding lines to read it and find the position of the name. But when I am trying to find the mass to the corresponding name as shown below it is not able to store it in the memory. In the Excel file, I have the mass values as 1.989*10^30. This seems to affect the code as the same code works fine when the cells in the excel has just numeric values.
majbod = 'Sun';
minbod = 'Earth';
majbodin = readtable("Major_and_Minor_Bodies.xlsx","Sheet",1);
minbodin = readtable("Major_and_Minor_Bodies.xlsx","Sheet",2);
MAJORBODY = table2array(majbodin(:,"Major_Body"));
MINORBODY = table2array(minbodin(:,"Minor_Body"));
mmaj = table2array(majbodin(:,"Mass"));
mmin = table2array(minbodin(:,"Mass"));
selected_majbody = find(strcmp(MAJORBODY,majbod));
selected_minbody = find(strcmp(MINORBODY,minbod));
M = mmaj(selected_majbody);
m = mmin(selected_minbody);
disp([M ;m])
Is there a better way to write the code compared to the way which I wrote?
Thanks.
Excel does it's best to figure out what kind of data is in each cell. Since your data has something besides just numbers, Excel treats it like a string. You have a couple of options for getting around that:
If you put an equals sign in front of it, it will treat it like an equation, and calculate the value of 1.989*10^3 for you. this will be a number.
Since scientific notation is so common, programmers have created a shortcut for representing it. They often use the character 'E' where you use "*10^". This means that if you type "1.989E30", excel will recognize that as a number.
If keeping the current string format is very important, you could probably modify the string during extraction - replace '*10^' with E, and then whatever language you are using will have a string to number parser you can use.
If the real problem is that the real numbers are just too long in Excel, you can always format the cell that they are in. (right click the cell, select format cells, then select scientific.)
Good luck
I am using Apache POI 3.9 for XLS/XLSX file processing.
In the XLS sheet, there is a column with numeric value like "3000053406".
When I read it with POI with..
cell.getNumericCellValue()
It gives me value like "3.00E+08". This create huge problem in my application.
How can I set the number formatting while reading data in Apcahe POI ?
There is a way that I know is to set the column as "text" type. But I want to know if there is any other way at Apache POI side while reading the data. OR can we format it by using simple java DecimalFormatter ?
This one comes up very often....
Picking one of my past answers to an almost identical question
What you want to do is use the DataFormatter class. You pass this a cell, and it does its best to return you a string containing what Excel would show you for that cell. If you pass it a string cell, you'll get the string back. If you pass it a numeric cell with formatting rules applied, it will format the number based on them and give you the string back.
For your case, I'd assume that the numeric cells have an integer formatting rule applied to them. If you ask DataFormatter to format those cells, it'll give you back a string with the integer string in it.
Problem can be strictly Java-related, not POI related, too.
Since your call returns a double,
double val = cell.getNumericCellValue();
You may want to get this
DecimalFormat df = new DecimalFormat("#");
int fractionalDigits = 2; // say 2
df.setMaximumFractionDigits(fractionalDigits);
double val = df.format(val);
Creating a BigDecimal with the double value from the numeric cell and then using the
BigDecimal.toPlainString()
function to convert it to a plain string and then storing it back to the same cell after erasing the value solved the whole problem of exponential representation of numeric values.
The below code solved the issue for me.
Double dnum = cellContent.getNumericCellValue();
BigDecimal bd = new BigDecimal(dnum);
System.out.println(bd.toPlainString());
cellContent.setBlank();
cellContent.setCellValue(bd.toPlainString());
System.out.println(cellContent.getStringCellValue());
long varA = new Double(cellB1.getNumericCellValue()).longValue();
This will bring the exact value in variable varA.
I am putting a string into excel. The string is often only numeric digits but can have alpha characters or hypens etc.
When I don't set the number format or set it like this
(Where xlSheet(0) is Excel.Worksheet)
xlSheet(0).Columns("N:N").EntireColumn.Columns.NumberFormat = "#"
It outputs in scientific notation.
When I use this code:
xlSheet(0).Columns("N:N").EntireColumn.Columns.NumberFormat = "0"
It rounds up the number to the nearest 100,000 so that the last five digits are 0's when they shouldn't be.
Should be: 1539648751235678942
But is: 1539648751235600000
The cells that have a hyphen or a letter aren't affected and work fine.
Any help would be greatly appreciated.
EDIT:
I add the data like this:
I loop through and put in xlSheet(0).Cells(i, 14) = rs!value_number
Where rs is my ADODB.Recordset
EDIT2: Herbert Sitz got it by adding an apostrophe before the text! Thanks everyone.
I think problem is that the number you're trying to enter can't be accommodated exactly by Excel. Excel has limitations on what numbers it display/represent because of the way numbers are stored internally. In Excel's case numbers are limited to 15 digit precision (see http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010073849.aspx ), which is not enough to represent your number.
You can enter the number as a string ("152..42") and all digits will be displayed, but you won't be able to perform exact mathematical operations with it.
For numbers, Excel can only handle 15 significant digits.
If you want to store a number that is more than 15 digits long without losing data, you have to store the data as text.
Doing what you've been doing will resolve the issue:
You can do either of the following to add your numbers as text:
xlSheet(0).Cells(i, 14).Numberformat = "#"
xlSheet(0).Cells(i, 14) = rs!value_number
Or
xlSheet(0).Cells(i, 14) = "'" & rs!value_number
I have price column in which the prices are displayed in 3 decimals and 4 decimals places, for example 123.456 or 123.4357.
So irrespective of the number of decimal places i want the value to be with only two decimals.
So i am selecting the column and in the VBA i am using Selection.NumberFormat = "0.00"
Which is resulting me the rounded value i.e when i format 123.456 and 123.4357 to 0.00 i am getting 123.46 and 123.44 but i want it to be 123.46 and 123.43.
So just wondering is there a way we can just trim the values instead of rounding.
Please give me some sample examples.
Thank you in advance.
Excel has a built-in function, trunc, that should do the trick for you.
This is what I placed in a1:b2.
123.456 =trunc(A1,2)
123.4357 =trunc(A2,2)
This will display
123.456 123.45
123.4357 123.43
You could treat the result as a string and extract the number of characters you need as in:
dim a as double
dim s as string
a = 123.4357
s = MID(a,1,FIND(".",a))&MID(a,FIND(".",a)+1,2)
I don't believe there is a built in mask that will truncate without rounding, you could instead use another column containing =TRUNC(A1, 2).
I am having some trouble using the custom number format feature in Excel to display US Treasury futures quotes.
The format by which they are displayed in is:
121'167
Which is equivalent to 121 + 16.75/32
If the last digit is a 5, as in 121'165, then it is equivalent to
121 + 16.5/32
If the last digit is a 2, as in 121'162, then it is equivalent to
121 + 16.25/32
and finally if the last digit is a 0, as in 121'160, then it is equivalent to
121 + 16/32
Is there a way to implement this definition using the custom number formatting feature or is it beyond it's capability?
This is one insane format: add fractions, then use decimals in the dividend, and then truncate those decimals...
But enough of the ranting. This should do the job, if I understood your specification correctly:
=TRUNC(A1)&"'"&INT(MOD(A1,1)*320)
Number formats alone cannot do this, because they cannot do actual conversions of the numbers (apart from the built-ins like date/time).
you can apply the TEXT() function to both of your values and concatenate them into a single string.
example (A1 = 121, B1 = 16.75)
C1: =TEXT(A1,"###") & "'" & TEXT(B1*10-1,"###") & "/32"
You need to work out rounding (I put a constant *10-1 into the 2nd term) and how to obtain the "/32" ... I couldn't spot that from your question
Hope that helps
good luck
=TRUNC(A1)&"'"&ROUND((MOD(A1,1)*32),1)
Where A1 = 109.6406
This is what worked best for me. I simply had to take a 2-year note quote in the form of, as an example, 109'20.5 and replicate in excel. I converted the quote into 109.6406 (109 + 20.5/32) and applied the above formula.
I know this is a bit dated, but stumbled upon this today and needed the same information. Here is what I use, replace the cell across the formula to pull the quote.
5 yr:
=LEFT(J22,3)+(RIGHT(J22,3)/320)+IF(INT(RIGHT(J22,1))=2,0.0015625,IF(INT(RIGHT(J22,1))=7,0.0015625,0))
10 yr: =LEFT(B28,3)+(RIGHT(B28,3)/320)