Home All Groups Group Topic Archive Search About

Easy Q: 0.0 --> Output as string?

Author
18 Apr 2006 2:05 AM
Spam Catcher
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.

Author
18 Apr 2006 2:11 AM
Spam Catcher
Spam Catcher <spamhoneypot@rogers.com> wrote in
news:Xns97A8E0CB1A40Cusenethoneypotrogers@127.0.0.1:

> 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")
Author
18 Apr 2006 3:29 PM
Ken Halter
Show quote Hide quote
"Spam Catcher" <spamhoneypot@rogers.com> wrote in message
news:Xns97A8E1BFE18DCusenethoneypotrogers@127.0.0.1...
> Spam Catcher <spamhoneypot@rogers.com> wrote in
> news:Xns97A8E0CB1A40Cusenethoneypotrogers@127.0.0.1:
>
>> 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")

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 Halter - MS-MVP-VB (visiting from VB6 world) - http://www.vbsight.com
Please keep all discussions in the groups..
Author
18 Apr 2006 5:33 PM
Jim Wooley
Show quote Hide quote
> "Spam Catcher" <spamhoneypot@rogers.com> wrote in message
> news:Xns97A8E1BFE18DCusenethoneypotrogers@127.0.0.1...
>
>> Spam Catcher <spamhoneypot@rogers.com> wrote in
>> news:Xns97A8E0CB1A40Cusenethoneypotrogers@127.0.0.1:
>>> 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")
>>
> 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
Author
18 Apr 2006 11:13 PM
Spam Catcher
"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com> wrote in news:#QO
$kzvYGHA.1***@TK2MSFTNGP02.phx.gbl:

>  "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 ; )
Author
18 Apr 2006 12:06 PM
Phill W.
Spam Catcher wrote:

> 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.
Author
18 Apr 2006 5:55 PM
Chris Dunaway
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.
Author
18 Apr 2006 5:57 PM
Chris Dunaway
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