PDA

View Full Version : Formatting string number


MAX
10-13-2007, 05:33 AM
Hello,

I need to print only 2 decimals on the output.
Let's say I have this code:

int a = 5;
int b = 16;

double c = a / b;


the result is 0.3125.
If I want to print only 0.31, how should the correct print code be?

Thanks.

NinjaTrader_Dierk
10-13-2007, 06:56 AM
Please tryPrint(c.ToString("#.##"));

MAX
10-13-2007, 07:59 AM
I did, but it doesn't show any number.

NinjaTrader_Dierk
10-13-2007, 08:14 AM
The problem is that you divide 2 integers which in your case results in c = 0. Just make a and b of type double.

MAX
10-13-2007, 08:22 AM
I tried with double, but doesn't work.

NinjaTrader_Dierk
10-13-2007, 08:44 AM
I strongly recommend consulting the MS C# docs to understand the basics of .NET numerics operations and how to format numerics.

I just tested this code:
double a = 5;
double b = 16;
double c = a / b;
Print(c.ToString("#.##"));... and it prints out ",31" on my German Windows.

Try
c.ToString("0.##")... if you want to have a leading 0.

MAX
10-13-2007, 11:20 AM
Thanks. Now is working.