如何让我的双打显示存储它们的小数位数?
How can I get my doubles to display the decimals with which they are stored?
我有一个 class:
public class PairODocs
{
public string Whirred;
public int Doc1Count = 0;
public double Doc1Prcntg = 0.0;
public int Doc2Count = 0;
public double Doc2Prcntg = 0.0;
}
...我这样填充:
PairODocs pod;
List<String> slDistinctUncommonWords = new List<string>();
lstPairODocs = new List<PairODocs>();
int doc1Count = 0;
int doc2Count = 0;
double doc1Prcntg = 0.0;
double doc2Prcntg = 0.0;
try
{
slDistinctUncommonWords = GetDistinctWordsFromDB();
foreach (string whirred in slDistinctUncommonWords)
{
pod = new PairODocs();
doc1Count = GetDoc1CountFor(whirred);
doc2Count = GetDoc2CountFor(whirred);
doc1Prcntg = (double)doc1Count / iTotalCountOfWordsInDoc1;
doc2Prcntg = (double)doc2Count / iTotalCountOfWordsInDoc2; // * 100);
pod.Whirred = whirred;
pod.Doc1Count = doc1Count;
pod.Doc1Prcntg = Math.Round(doc1Prcntg, 7);
pod.Doc2Count = doc2Count;
pod.Doc2Prcntg = Math.Round(doc2Prcntg, 7);
lstPairODocs.Add(pod);
}
}
...然后像这样使用 iText 7 写入 PDF 文件:
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
foreach (PairODocs pod in lstPairODocs.OrderByDescending(a => a.Doc1Prcntg).ThenByDescending(a => a.Doc2Prcntg))
{
if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
{
Cell cell = new Cell();
cell.Add(new Paragraph(pod.Whirred));
cell.SetBackgroundColor(wordmatchHighlight);
table.AddCell(cell);
cell = new Cell();
cell.Add(new Paragraph(pod.Doc1Count.ToString()));
cell.SetBackgroundColor(wordmatchHighlight);
table.AddCell(cell);
cell = new Cell();
cell.Add(new Paragraph(pod.Doc1Prcntg.ToString("P", nfi)));
cell.SetBackgroundColor(wordmatchHighlight);
table.AddCell(cell);
cell = new Cell();
cell.Add(new Paragraph(pod.Doc2Count.ToString()));
cell.SetBackgroundColor(wordmatchHighlight);
table.AddCell(cell);
cell = new Cell();
cell.Add(new Paragraph(pod.Doc2Prcntg.ToString("P", nfi)));
cell.SetBackgroundColor(wordmatchHighlight);
table.AddCell(cell);
}
...但不是显示小数点后 7 位的值,而是只显示两位,如下所示:
由于有一个计数 (1),我不希望百分比为 0.00,而是 0.00001 或显示 1 不是零的小数点数。
为什么显示被限制为小数点后两位?更重要的是,我怎样才能修复它,让它扩展到更多?我不希望“计数”列中的任何值在百分比列中显示 0.00,除非相应的计数为 0。
Why is the display being restricted to 2 decimal places?
默认情况下,百分比格式说明符 P
是保留两位小数的百分比。如果需要 7 位小数,请使用 ToString("P7")
。
And more importantly, how can I fix it so that it will expand out to more?
我不太明白你所说的“修复它以便它扩展到更多”是什么意思 - 你想要它是固定的还是可变的?对于“至少 7 但最多 15 dp”,您可能必须使用 "0.0000000########%"
之类的东西(如果这就是 fixed/expanding 的意思)
你可以用这个代替 .ToString()
:
cell.Add(new Paragraph(string.Format("{0:N7}%",pod.Doc1Prcntg)));
我有一个 class:
public class PairODocs
{
public string Whirred;
public int Doc1Count = 0;
public double Doc1Prcntg = 0.0;
public int Doc2Count = 0;
public double Doc2Prcntg = 0.0;
}
...我这样填充:
PairODocs pod;
List<String> slDistinctUncommonWords = new List<string>();
lstPairODocs = new List<PairODocs>();
int doc1Count = 0;
int doc2Count = 0;
double doc1Prcntg = 0.0;
double doc2Prcntg = 0.0;
try
{
slDistinctUncommonWords = GetDistinctWordsFromDB();
foreach (string whirred in slDistinctUncommonWords)
{
pod = new PairODocs();
doc1Count = GetDoc1CountFor(whirred);
doc2Count = GetDoc2CountFor(whirred);
doc1Prcntg = (double)doc1Count / iTotalCountOfWordsInDoc1;
doc2Prcntg = (double)doc2Count / iTotalCountOfWordsInDoc2; // * 100);
pod.Whirred = whirred;
pod.Doc1Count = doc1Count;
pod.Doc1Prcntg = Math.Round(doc1Prcntg, 7);
pod.Doc2Count = doc2Count;
pod.Doc2Prcntg = Math.Round(doc2Prcntg, 7);
lstPairODocs.Add(pod);
}
}
...然后像这样使用 iText 7 写入 PDF 文件:
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
foreach (PairODocs pod in lstPairODocs.OrderByDescending(a => a.Doc1Prcntg).ThenByDescending(a => a.Doc2Prcntg))
{
if ((pod.Doc1Count > 0) && (pod.Doc2Count > 0))
{
Cell cell = new Cell();
cell.Add(new Paragraph(pod.Whirred));
cell.SetBackgroundColor(wordmatchHighlight);
table.AddCell(cell);
cell = new Cell();
cell.Add(new Paragraph(pod.Doc1Count.ToString()));
cell.SetBackgroundColor(wordmatchHighlight);
table.AddCell(cell);
cell = new Cell();
cell.Add(new Paragraph(pod.Doc1Prcntg.ToString("P", nfi)));
cell.SetBackgroundColor(wordmatchHighlight);
table.AddCell(cell);
cell = new Cell();
cell.Add(new Paragraph(pod.Doc2Count.ToString()));
cell.SetBackgroundColor(wordmatchHighlight);
table.AddCell(cell);
cell = new Cell();
cell.Add(new Paragraph(pod.Doc2Prcntg.ToString("P", nfi)));
cell.SetBackgroundColor(wordmatchHighlight);
table.AddCell(cell);
}
...但不是显示小数点后 7 位的值,而是只显示两位,如下所示:
由于有一个计数 (1),我不希望百分比为 0.00,而是 0.00001 或显示 1 不是零的小数点数。
为什么显示被限制为小数点后两位?更重要的是,我怎样才能修复它,让它扩展到更多?我不希望“计数”列中的任何值在百分比列中显示 0.00,除非相应的计数为 0。
Why is the display being restricted to 2 decimal places?
默认情况下,百分比格式说明符 P
是保留两位小数的百分比。如果需要 7 位小数,请使用 ToString("P7")
。
And more importantly, how can I fix it so that it will expand out to more?
我不太明白你所说的“修复它以便它扩展到更多”是什么意思 - 你想要它是固定的还是可变的?对于“至少 7 但最多 15 dp”,您可能必须使用 "0.0000000########%"
之类的东西(如果这就是 fixed/expanding 的意思)
你可以用这个代替 .ToString()
:
cell.Add(new Paragraph(string.Format("{0:N7}%",pod.Doc1Prcntg)));