user270124
user270124

Reputation: 537

String formatting: preserve all digits before point, but truncate after point

How do I format this number in the following way?

The digits before the point should be preserved, but the digits after the point should be truncated to the first three digits.

Example:

double example1 = 0.7221025; // Wanted: 0.722
double example2 = 1.2300001; // Wanted: 1.230
double example3 = 1.000000001; // Wanted: 1.000

I have tried using String.Format, but I have trouble getting the correct formatter.

Upvotes: 0

Views: 70

Answers (2)

postgresnewbie
postgresnewbie

Reputation: 1468

if you are insisting to use

String.Format

Then you can use it like this example:

String.Format("{0:0.000}", example1)

Upvotes: 0

Related Questions