AjayKumar01 / excellibrary

Automatically exported from code.google.com/p/excellibrary
0 stars 0 forks source link

Rounding of number to 2 decimal places #39

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Add a number with more than 1 decimal place (using code: .Cells(0, 0) = 
New ExcelLibrary.SpreadSheet.Cell(1.2345)
2. Save spreadsheet 
3. Number shows as 1.23 (rounds up or down)

What is the expected output? What do you see instead?
1.2345

What version of the product are you using? On what operating system?
Latest dll, VB2005, XP

Please provide any additional information below.

Original issue reported on code.google.com by simonsam...@googlemail.com on 4 Aug 2009 at 3:53

GoogleCodeExporter commented 9 years ago
the problem is in the 
ExcelLibrary\Office\Excel\BinaryFileFormat\Encode\WorkSheetEncoder.cs file

Changing the encodecell routine to be like the one below resolved my issue with 
the library.

private static CellValue EncodeCell(Cell cell, SharedResource sharedResource)
        {
            object value = cell.Value;
            if (value is int || value is short)
            {
                RK rk = new RK();
                rk.Value = (uint)(Convert.ToInt32(value) << 2 | 2);
                return rk;
            }
            else if (value is decimal)
            {
                //if (Math.Abs((decimal)value) <= (decimal)5368709.11)
                //{
                //    RK rk = new RK();
                //    rk.Value = (uint)((int)((decimal)value * 100) << 2 | 3); // integer and mul
                //    return rk;
                //}
                //else
                //{
                    NUMBER number = new NUMBER();
                    number.Value = (double)(decimal)value;
                    return number;
                //}
            }
            else if (value is double)
            {
                //RK rk = new RK();
                //Int64 data = BitConverter.DoubleToInt64Bits((double)value);
                //rk.Value = (uint)(data >> 32) & 0xFFFFFFFC;
                //return rk;
                NUMBER number = new NUMBER();
                number.Value = (double)value;
                return number;
            }
            else if (value is string)
            {
                LABELSST label = new LABELSST();
                label.SSTIndex = sharedResource.GetSSTIndex((string)value);
                return label;
            }
            else if (value is DateTime)
            {
                NUMBER number = new NUMBER();
                number.Value = sharedResource.EncodeDateTime((DateTime)value);
                return number;
            }
            else if (value is bool)
            {
                BOOLERR boolerr = new BOOLERR();
                boolerr.ValueType = 0;
                boolerr.Value = Convert.ToByte((bool)value);
                return boolerr;
            }
            else if (value is ErrorCode)
            {
                BOOLERR boolerr = new BOOLERR();
                boolerr.ValueType = 1;
                boolerr.Value = ((ErrorCode)value).Code;
                return boolerr;
            }
            else
            {
                throw new Exception("Invalid cell value.");
            }
        }

Original comment by lewisb...@gmail.com on 10 Jun 2010 at 4:24

GoogleCodeExporter commented 9 years ago
lewis:

I did exactly what you have done here. It works perfectly. However I would like 
to know why the code looked like that. I can not understand it, but it has to 
be a reason, that's for sure.
Is it going to be a new release containing these modifications in a near future 
?

Thanks in advance.
Ariel.

Original comment by alejandr...@gmail.com on 22 Aug 2012 at 12:05