|
| 1 | +--- |
| 2 | +title: How to count decimal places in an Excel cell value | Syncfusion |
| 3 | +description: Code examples to count decimal places in a numeric cell value using Syncfusion .NET Excel library (XlsIO). |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# How to count decimal places in an Excel cell value? |
| 10 | + |
| 11 | +The following code examples demonstrate counting the number of decimal places in a numeric cell value using C# (Cross-platform and Windows-specific) and VB.NET. |
| 12 | + |
| 13 | +{% tabs %} |
| 14 | +{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/FAQ/Decimal %20Places%20Count/.NET/DecimalPlacesCount/DecimalPlacesCount/Program.cs,180" %} |
| 15 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 16 | +{ |
| 17 | + IApplication application = excelEngine.Excel; |
| 18 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 19 | + IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx")); |
| 20 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 21 | + |
| 22 | + // Get the cell text safely |
| 23 | + string cellText = worksheet.Range["G2"].Value?.ToString() ?? string.Empty; |
| 24 | + |
| 25 | + // Count decimal places: if there's a decimal point, count chars after it; otherwise 0 |
| 26 | + int countDecimalPlaces = 0; |
| 27 | + int dotIndex = cellText.IndexOf('.'); |
| 28 | + if (dotIndex >= 0) |
| 29 | + { |
| 30 | + countDecimalPlaces = cellText.Length - dotIndex - 1; |
| 31 | + } |
| 32 | + |
| 33 | + // Display result in console |
| 34 | + Console.WriteLine(countDecimalPlaces); |
| 35 | + |
| 36 | + //Save the workbook |
| 37 | + workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx")); |
| 38 | +} |
| 39 | +{% endhighlight %} |
| 40 | + |
| 41 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 42 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 43 | +{ |
| 44 | + IApplication application = excelEngine.Excel; |
| 45 | + application.DefaultVersion = ExcelVersion.Xlsx; |
| 46 | + IWorkbook workbook = application.Workbooks.Open("Input.xlsx"); |
| 47 | + |
| 48 | + // Get the cell text safely |
| 49 | + string cellText = workbook.Worksheets[0].Range["G2"].Value?.ToString() ?? string.Empty; |
| 50 | + |
| 51 | + // Count decimal places |
| 52 | + int countDecimalPlaces = 0; |
| 53 | + int dotIndex = cellText.IndexOf('.'); |
| 54 | + if (dotIndex >= 0) |
| 55 | + { |
| 56 | + countDecimalPlaces = cellText.Length - dotIndex - 1; |
| 57 | + } |
| 58 | + |
| 59 | + // Display result in console |
| 60 | + Console.WriteLine(countDecimalPlaces); |
| 61 | + |
| 62 | + //Save the workbook |
| 63 | + workbook.SaveAs("Output.xlsx"); |
| 64 | +} |
| 65 | +{% endhighlight %} |
| 66 | + |
| 67 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 68 | +Using excelEngine As New ExcelEngine() |
| 69 | + Dim application As IApplication = excelEngine.Excel |
| 70 | + application.DefaultVersion = ExcelVersion.Xlsx |
| 71 | + Dim workbook As IWorkbook = application.Workbooks.Open("Input.xlsx") |
| 72 | + Dim worksheet As IWorksheet = workbook.Worksheets(0) |
| 73 | + |
| 74 | + ' Get the cell text safely |
| 75 | + Dim cellText As String = If(worksheet.Range("G2").Value IsNot Nothing, worksheet.Range("G2").Value.ToString(), String.Empty) |
| 76 | + |
| 77 | + ' Count decimal places |
| 78 | + Dim countDecimalPlaces As Integer = 0 |
| 79 | + Dim dotIndex As Integer = cellText.IndexOf("."c) |
| 80 | + If dotIndex >= 0 Then |
| 81 | + countDecimalPlaces = cellText.Length - dotIndex - 1 |
| 82 | + End If |
| 83 | + |
| 84 | + ' Display result in console |
| 85 | + Console.WriteLine(countDecimalPlaces) |
| 86 | + |
| 87 | + 'Save the workbook |
| 88 | + workbook.SaveAs("Output.xlsx") |
| 89 | +End Using |
| 90 | +{% endhighlight %} |
| 91 | +{% endtabs %} |
| 92 | + |
| 93 | +A complete working example in C# is present on <a href="https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/FAQ/Decimal %20Places%20Count/.NET/DecimalPlacesCount">this GitHub page</a>. |
0 commit comments