PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 10, 2022

[FIXED] How can I get my doubles to display the decimals with which they are stored?

 August 10, 2022     c#, decimal, double, itext7     No comments   

Issue

I have a 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;
}

...that I populate like so:

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);
    }
}

...and then write to a PDF file using iText 7 like so:

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);
}

...but instead of showing the values out to 7 decimal places, it only displays two, like so:

enter image description here

Since there is a Count (1), I don't want Percentage to be 0.00, but 0.00001 or however many decimal points it takes being displayed to show that 1 is not zilch.

Why is the display being restricted to 2 decimal places? And more importantly, how can I fix it so that it will expand out to more? I don't want any value in the "Count" column to display 0.00 in the percentage column unless the corresponding Count is 0.


Solution

Why is the display being restricted to 2 decimal places?

By default the percent format specifier P is a percent to two decimal places. Use ToString("P7") if you want 7 decimal places.

And more importantly, how can I fix it so that it will expand out to more?

I didn't quite get what you meant by "fix it so it expands to more" - do you want it fixed or variable? You might have to use something like "0.0000000########%" for "at least 7 but up to 15 dp" (if that's what you meant by fixed/expanding)



Answered By - Caius Jard
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing