Tassisto
Tassisto

Reputation: 10345

How to add trailing zero's in a string with c#?

I have amounts with decimals (300.11 and 9801.98), I export them Excel and everything is fine.

But sometimes I have amounts with trailing zeros, and I want to keep those zeros when exporting? For example 300.00 becomes 300 when exporting to Excel. How can I fix this?

This is the code right before the export happens.

AmountCCC is of type double.

row.Amount = row.AmountCCC.ToString(CultureInfo.InvariantCulture);

See in the screenshot how the records are exported.

enter image description here

Upvotes: 2

Views: 1871

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460208

You can use N2

row.AmountCCC.ToString("N2", CultureInfo.InvariantCulture);

The Numeric ("N") Format Specifier

Upvotes: 2

Related Questions