|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Easy Q: 0.0 --> Output as string?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. Spam Catcher <spamhoneypot@rogers.com> wrote in
news:Xns97A8E0CB1A40Cusenethoneypotrogers@127.0.0.1: Solved it... used Format instead. Format(0.0, "0.0")> 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. >
Show quote
Hide quote
"Spam Catcher" <spamhoneypot@rogers.com> wrote in message One thing to be aware of (if not already), you'll get different results from 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") 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..
Show quote
Hide quote
> "Spam Catcher" <spamhoneypot@rogers.com> wrote in message Ken is correct. You can supply an overload to the Round method to force it > 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. 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 "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 Ah perfect - I like math rounding more ; )> <g>) while Format uses the same method of rounding I learned in math > class... which is, everything >=5 is rounded up. > > Spam Catcher wrote:
> I'm trying to output 0.0 as a string ("0.0"). How about this?> When I go: Math.Round(0.0, 2).ToString() it is outputting as 0. Math.Round(0.0, 2).ToString("0.0") HTH, Phill W. 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 |
|||||||||||||||||||||||