Back to Devexpress

Formatting.NumberFormat Property

officefileapi-devexpress-dot-spreadsheet-dot-formatting-60eb6869.md

latest10.6 KB
Original Source

Formatting.NumberFormat Property

Specifies a number format for a numeric cell value in the invariant culture.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
string NumberFormat { get; set; }
vb
Property NumberFormat As String

Property Value

TypeDescription
String

The number format in the invariant (English) culture.

|

Remarks

The Spreadsheet uses Microsoft Excel number formats to display cell numbers as percentage, decimal, currency, accounting, and date/time values.

The NumberFormat property allows you to use both the built-in and custom number formats. Refer to the following example for details: How to: Specify Number or Date Format for Cell Content.

You can also use the Formatting.NumberFormatLocal property to obtain or specify a number format based on the local culture defined by the DocumentOptions.Culture property.

Example

This example demonstrates how to use different number formats to display numeric values in cells.

View Example

csharp
// Display 111 as 111.
worksheet.Cells["A1"].Value = 111;
worksheet.Cells["A1"].NumberFormat = "#####";

// Display 222 as 00222.
worksheet.Cells["B1"].Value = 222;
worksheet.Cells["B1"].NumberFormat = "00000";

// Display 12345678 as 12,345,678.
worksheet.Cells["C1"].Value = 12345678;
worksheet.Cells["C1"].NumberFormat = "#,#";

// Display .126 as 0.13.
worksheet.Cells["D1"].Value = .126;
worksheet.Cells["D1"].NumberFormat = "0.##";

// Display 74.4 as 74.400.
worksheet.Cells["E1"].Value = 74.4;
worksheet.Cells["E1"].NumberFormat = "##.000";

// Display 1.6 as 160.0%.
worksheet.Cells["F1"].Value = 1.6;
worksheet.Cells["F1"].NumberFormat = "0.0%";

// Display 4321 as $4,321.00.
worksheet.Cells["G1"].Value = 4321;
worksheet.Cells["G1"].NumberFormat = "$#,##0.00";

// Display 8.75 as 8 3/4.
worksheet.Cells["H1"].Value = 8.75;
worksheet.Cells["H1"].NumberFormat = "# ?/?";
vb
' Display 111 as 111.
worksheet.Cells("A1").Value = 111
worksheet.Cells("A1").NumberFormat = "#####"

' Display 222 as 00222.
worksheet.Cells("B1").Value = 222
worksheet.Cells("B1").NumberFormat = "00000"

' Display 12345678 as 12,345,678.
worksheet.Cells("C1").Value = 12345678
worksheet.Cells("C1").NumberFormat = "#,#"

' Display .126 as 0.13.
worksheet.Cells("D1").Value =.126
worksheet.Cells("D1").NumberFormat = "0.##"

' Display 74.4 as 74.400.
worksheet.Cells("E1").Value = 74.4
worksheet.Cells("E1").NumberFormat = "##.000"

' Display 1.6 as 160.0%.
worksheet.Cells("F1").Value = 1.6
worksheet.Cells("F1").NumberFormat = "0.0%"

' Display 4321 as $4,321.00.
worksheet.Cells("G1").Value = 4321
worksheet.Cells("G1").NumberFormat = "$#,##0.00"

' Display 8.75 as 8 3/4.
worksheet.Cells("H1").Value = 8.75
worksheet.Cells("H1").NumberFormat = "# ?/?"

The following code snippets (auto-collected from DevExpress Examples) contain references to the NumberFormat property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

spreadsheet-document-api-examples-part1/CS/SpreadsheetExamples/SpreadsheetActions/CellActions.cs#L83

csharp
worksheet.Cells["B7"].SetValueFromText("2147483647");
worksheet.Cells["B8"].NumberFormat = "###.###";
worksheet.Cells["B8"].SetValueFromText("27-Jul-16 5:43PM", true);

winforms-spreadsheetcontrol-api-part1/CS/SpreadsheetControl/SpreadsheetActions/FormattingActions.cs#L166

csharp
// Apply different date display formats.
worksheet.Cells["A1"].NumberFormat = "m/d/yy";

wpf-spreadsheetcontrol-api-part-1/CS/SpreadsheetControl_WPF_API/SpreadsheetActions/CellActions.cs#L155

csharp
sourceRange[2].Value = DateTime.Now;
sourceRange[2].NumberFormat = "d-mmm-yy";

wpf-spreadsheetcontrol-api-part-2/CS/SpreadsheetControl_WPF_API_Part02/SpreadsheetActions/DataValidationActions.cs#L27

csharp
worksheet["C1"].SetValue(DateTime.Now);
worksheet["C1"].NumberFormat = "mmm/d/yyyy h:mm";

winforms-spreadsheetcontrol-api-part-2/CS/SpreadsheetControl_API_Part02/SpreadsheetActions/CustomFunctionActions.cs#L48

csharp
worksheet["C2"].FormulaInvariant = "=SPHEREMASS(A2)";
worksheet["C2"].NumberFormat = "#.##";
worksheet["A3"].Value = 0.1;

spreadsheet-document-api-examples-part1/VB/SpreadsheetExamples/SpreadsheetActions/DocumentPropertiesActions.vb#L77

vb
worksheet(rowIndex, 2).Value = workbook.DocumentProperties.Custom(propertyName)
If worksheet(rowIndex, 2).Value.IsDateTime Then worksheet(rowIndex, 2).NumberFormat = "[$-409]m/d/yyyy h:mm AM/PM"
rowIndex += 1

winforms-spreadsheetcontrol-api-part1/VB/SpreadsheetControl/SpreadsheetActions/CellActions.vb#L159

vb
worksheet.Cells("B7").SetValueFromText("2147483647")
worksheet.Cells("B8").NumberFormat = "###.###"
worksheet.Cells("B8").SetValueFromText("27-Jul-16 5:43PM", True)

wpf-spreadsheetcontrol-api-part-1/VB/SpreadsheetControl_WPF_API/SpreadsheetActions/CellActions.vb#L144

vb
sourceRange(2).Value = Date.Now
sourceRange(2).NumberFormat = "d-mmm-yy"

winforms-spreadsheetcontrol-api-part-2/VB/SpreadsheetControl_API_Part02/SpreadsheetActions/CustomFunctionActions.vb#L49

vb
worksheet("C2").FormulaInvariant = "=SPHEREMASS(A2)"
worksheet("C2").NumberFormat = "#.##"
worksheet("A3").Value = 0.1

wpf-spreadsheetcontrol-api-part-2/VB/SpreadsheetControl_WPF_API_Part02/SpreadsheetActions/CustomFunctionActions.vb#L45

vb
worksheet("C2").FormulaInvariant = "=SPHEREMASS(A2)"
worksheet("C2").NumberFormat = "#.##"
worksheet("A3").Value = 0.1

See Also

NumberFormatLocal

Cell Data Types

Dates and Times in Cells

Formatting Interface

Formatting Members

DevExpress.Spreadsheet Namespace