Easy Q: 0.0 --> Output as string?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

I'm trying to output 0.0 as a string ("0.0"). When I go: Math.Round(0.0,
2).ToString() it is outputting as 0.

Any ideas how to get the extra 0 after the decimal point?

Thanks.
 
Hi all,

I'm trying to output 0.0 as a string ("0.0"). When I go: Math.Round(0.0,
2).ToString() it is outputting as 0.

Any ideas how to get the extra 0 after the decimal point?

Thanks.


Solved it... used Format instead. Format(0.0, "0.0")
 
Spam said:
I'm trying to output 0.0 as a string ("0.0").
When I go: Math.Round(0.0, 2).ToString() it is outputting as 0.

How about this?

Math.Round(0.0, 2).ToString("0.0")

HTH,
Phill W.
 
Spam Catcher said:
Solved it... used Format instead. Format(0.0, "0.0")

One thing to be aware of (if not already), you'll get different results from
Format vs Round. "Round" uses bankers rounding (which makes no sense to me
<g>) while Format uses the same method of rounding I learned in math
class... which is, everything >=5 is rounded up.
 
Spam Catcher said:
One thing to be aware of (if not already), you'll get different
results from Format vs Round. "Round" uses bankers rounding (which
makes no sense to me <g>) while Format uses the same method of
rounding I learned in math class... which is, everything >=5 is
rounded up.

Ken is correct. You can supply an overload to the Round method to force it
to behave the same as the format command. Use the MidpointRounding.AwayFromZero
overload. See my write-up at http://devauthority.com/blogs/jwooley/archive/2006/03/24/806.aspx.

Jim Wooley
http://devauthority.com/blogs/jwooley
 
What is the data type of the value? If it is Decimal for example, you
can use this:

Decimal d = 0.0m;

MessageBox.Show(d.ToString("N1"); //Outputs 0.0
MessageBox.Show(d.ToString("C2"); //Outputs $0.00

It would help to know the context in which the value is used.
 
Oops! Forgot where I was:

Dim d As Decimal = 0.00

MessageBox.Show(d.ToString("N1")) 'Outputs 0.0
MessageBox.Show(d.ToString("C2")) 'Outputs $0.00
 
"Round" uses bankers rounding (which makes no sense to me
<g>) while Format uses the same method of rounding I learned in math
class... which is, everything >=5 is rounded up.

Ah perfect - I like math rounding more ; )
 
Back
Top