views:

37

answers:

1

I'm using NPOI to generate XLS spreadsheets. NPOI is an Excel Spreadsheet generation library/API that is available on codeplex, enables you to create workbooks, formatting, formulae and so on and so forth....I use it to create workbooks with multiple sheets that contain the output of the various calculations.

I've used the following custom data format for each cell that contains a value, which I've plumbed into NPOI using code along the lines of:

var newFormat = MyNPOIWorkBook.CreateDataFormat();
var customFormat = newFormat.GetFormat("[=0]0;0.####");

the customFormat is then applied to a Cell within a method that creates the Cell Styles.

This works rather well - but not so well when the 4th decimal place after the point/period is a 0. When this is the case, I do not get the 0. My requirement is to have the 0 actually show!

So, to illustrate. 0.33445566 displays in the spreadsheet as 0.3343 (fine!) - but 0.3340 displays as 0.334 (not fine) - I want 0.3340. I appreciate that this is somewhat trivial, but I would like to satisfy my objective precisely :)

Can anybody help - either by suggesting a workaround, or altering my custom format in some magical fashion?

Thanks -SB

+1  A: 

How about [=0]0;0.###0?

Kyle Lowry
Would that not always give me a 0 as the 4th digit, even if the digit is 1,2,3,4,5,6,7,8 or 9?
SpaceBison
Nope - it is used to preserve a trailing zero. If the fourth digit is not zero, it will be displayed as normal. If the fourth digit is a zero, then the zero is displayed. Kind of weird, I know.
Kyle Lowry
I will give this a go - cheers :)
SpaceBison
Excellent - that works :) - Accepted!
SpaceBison