There is tblData in SQL which has following column Amount which is numeric
tblData
Amount
50.000000
0.882700
49.370000
have written a code to generate csv file
but in csv file this is coming as
Amount
50
0.8827
49.37
i want
50.000000
0.882700
49.370000
how can this be done? please guide
streamwritter is used to generate csv


Mithilesh TataRank #862 · 2.6K reputationPosted Mar 9, 2024, 1:21 AM EST
To ensure that numeric values are written with specific formatting, such as keeping trailing zeros, when writing to a CSV file using StreamWriter in C#, you can specify the formatting options while writing the data. Here's a sample code snippet demonstrating how you can achieve this:
using System; using System.IO; using System.Globalization; class Program { static void Main(string[] args) { // Sample data double[] amounts = { 50.000000, 0.882700, 49.370000 }; // File path string filePath = "output.csv"; // Write data to CSV file using (StreamWriter writer = new StreamWriter(filePath)) { // Write header writer.WriteLine("Amount"); // Write data rows foreach (double amount in amounts) { // Write formatted amount writer.WriteLine(amount.ToString("0.000000", CultureInfo.InvariantCulture)); } } Console.WriteLine("CSV file generated successfully."); } }In this code:
amount.ToString("0.000000", CultureInfo.InvariantCulture)is used to format theamountwith six decimal places and using the invariant culture to ensure consistent formatting across different systems.StreamWriteris used to write the formatted amounts to the CSV file.This should ensure that numeric values are written with the specified formatting to the CSV file.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 10, 2024, 12:56 PM EDT
we can make the code more flexible by allowing users to specify the number of decimal places for formatting. Here's the improved version:
using System; using System.IO; using System.Globalization; class Program { static void Main(string[] args) { // Sample data double[] amounts = { 50.000000, 0.882700, 49.370000 }; // File path string filePath = "output.csv"; // Write data to CSV file WriteDataToCSV(filePath, amounts, decimalPlaces: 6); Console.WriteLine("CSV file generated successfully."); } static void WriteDataToCSV(string filePath, double[] data, int decimalPlaces) { // Create StreamWriter with specified file path using (StreamWriter writer = new StreamWriter(filePath)) { // Write header writer.WriteLine("Amount"); // Write data rows with specified formatting foreach (double value in data) { // Format the value with specified number of decimal places string formattedValue = FormatNumber(value, decimalPlaces); // Write formatted value to CSV file writer.WriteLine(formattedValue); } } } static string FormatNumber(double number, int decimalPlaces) { // Construct format string based on the number of decimal places string format = "0." + new string('0', decimalPlaces); // Format the number with specified decimal places using InvariantCulture return number.ToString(format, CultureInfo.InvariantCulture); } }FormatNumberto encapsulate the formatting logic, making the code more modular and easier to understand.WriteDataToCSVto handle the process of writing data to the CSV file, enhancing code readability and maintainability.