Main Content

Format Numbers in Tables

This example shows how to format numbers in a table that is generated by a report generation program. The example creates a table of uniformly distributed random numbers that have a precision of three digits after the decimal point.

The numbers are formatted by using an mlreportgen.dom.NumberFormat style object with the table.

Generate Random Numbers

Generate a 4-by-4 array of random numbers. Initialize the random number generator using a seed of 1, so that each time the example runs, rand produces the same numbers.

format long
randNumbers = rand(4)
randNumbers = 4×4

   0.417022004702574   0.146755890817113   0.396767474230670   0.204452249731517
   0.720324493442158   0.092338594768798   0.538816734003357   0.878117436390945
   0.000114374817345   0.186260211377671   0.419194514403295   0.027387593197926
   0.302332572631840   0.345560727043048   0.685219500396759   0.670467510178402

The numbers display with a precision of 15 digits after the decimal point.

Create a Document

Import the DOM namespace so that you do not have to use long, fully-qualified class names.

import mlreportgen.dom.*

Create a PDF document. To create a Microsoft® Word, HTML, or single-file HTML document, change "pdf" to "docx", "html", or "html-file", respectively.

d = Document("randomnumbers","pdf");

Create a Table

Create a DOM table from the array of random numbers.

t = Table(randNumbers);

Specify the Table Style

Specify that numbers in the table have a precision of 3 digits after the decimal point by using an mlreportgen.dom.NumberFormat object. Specify the table width, border, and column and row separators.

t.Style = [t.Style 

Center the table entries in the table cells.

t.TableEntriesHAlign = "center";

Generate the Report

Append the table to the document. Close and view the document.


See Also

| |

Related Topics