Are there any C# guys on?

HJB417

Senior member
Dec 31, 2000
763
0
0
Console.WriteLine("{0}{1}{2}", Math.PI.ToString("F2"), Environment.NewLine, Math.PI.ToString("F3"));
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Also , you could use some creative math and casting to cut the numbers off

Psudocode:

1) Store 94.2904 as float in myFloat
2) Multiply it by 100 and store the result back in myFloat
3) Cast it to an integer to truncate the numbers to the right of the decimal place, store as intVal
4) Store the value in intVal as a double in myFloat
5) Divide myFloat by 100 and store it in myFloat

You should now have 94.29 in myFloat
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: amdfanboy
Also , you could use some creative math and casting to cut the numbers off

Psudocode:

1) Store 94.2904 as float in myFloat
2) Multiply it by 100 and store the result back in myFloat
3) Cast it to an integer to truncate the numbers to the right of the decimal place, store as intVal
4) Store the value in intVal as a double in myFloat
5) Divide myFloat by 100 and store it in myFloat

You should now have 94.29 in myFloat

if you divide an int by an int you will get an int regardless what type your assignment variable is. you would have to type cast also.

edit: nevermind didn't read step 4
 

HJB417

Senior member
Dec 31, 2000
763
0
0
Originally posted by: Jumpem
Cool. Can you explain what that's doing or point me to someplace good?

NumberFormatInfo Class

I did the 'Fixed-point format', and I also had a digit (2 and 3 in the above example) to tell it how many digits to output. What I stated is the simplest way to do what you want
someDouble.ToString("Fx") where X is the number of digits on the right hand side you want ouputed, in your case, 2 or 3.
someDouble.ToString("F2");
someDouble.ToString("F3");
 

Jumpem

Lifer
Sep 21, 2000
10,757
3
81
Originally posted by: HJB417
Originally posted by: Jumpem
Cool. Can you explain what that's doing or point me to someplace good?

NumberFormatInfo Class

I did the 'Fixed-point format', and I also had a digit (2 and 3 in the above example) to tell it how many digits to output. What I stated is the simplest way to do what you want
someDouble.ToString("Fx") where X is the number of digits on the right hand side you want ouputed, in your case, 2 or 3.
someDouble.ToString("F2");
someDouble.ToString("F3");

Thanks. Does it do any rounding when it truncates?
 

mugs

Lifer
Apr 29, 2003
48,920
46
91
Originally posted by: amdfanboy
Also , you could use some creative math and casting to cut the numbers off

Psudocode:

1) Store 94.2904 as float in myFloat
2) Multiply it by 100 and store the result back in myFloat
3) Cast it to an integer to truncate the numbers to the right of the decimal place, store as intVal
4) Store the value in intVal as a double in myFloat
5) Divide myFloat by 100 and store it in myFloat

You should now have 94.29 in myFloat


Step 2.5 - add 0.5 if you'd like it to round rather than truncate.
 

torpid

Lifer
Sep 14, 2003
11,631
11
76
What the... why do all the work manually when you have a function library that does it for you? Doesn't make any sense...
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: torpid
What the... why do all the work manually when you have a function library that does it for you? Doesn't make any sense...

Hypothetically speaking thata is what you could do if one didn't exist.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
This probably would've been useful for the programming assignment, if I had seen it earlier. Oh well =)
 

HJB417

Senior member
Dec 31, 2000
763
0
0
Originally posted by: Jumpem
Originally posted by: HJB417
Originally posted by: Jumpem
Cool. Can you explain what that's doing or point me to someplace good?

NumberFormatInfo Class

I did the 'Fixed-point format', and I also had a digit (2 and 3 in the above example) to tell it how many digits to output. What I stated is the simplest way to do what you want
someDouble.ToString("Fx") where X is the number of digits on the right hand side you want ouputed, in your case, 2 or 3.
someDouble.ToString("F2");
someDouble.ToString("F3");

Thanks. Does it do any rounding when it truncates?

rounding is performed.