locked
A Problem Using PadLeft

    Question

  • I was introduced to PadLeft in an earlier forum question but can't get it to do what I need. I'm using Visual Basic 2008 Express.

    Working with a couple of Type double variables I am calculationg a Type double result. I am then rounding the value to one decimal place, using Math.Round. Finally I'm trying to avoid the appearance of, for example, .8 The answer may turn out to be .8 but I'd rather show it as 0.8 to avoid confusion. I'm trying to use PadLeft to insert the necessary zero befor the decimal point and believe that to do this I must convert the rounded decimal number to a string first. The expression I end up with is

    Str(Math.Round(((5 - ms.mmoll(10)) / ms.ce), 1)).PadLeft(3, "0"c)

    I still get .8 as the result! I'm not sure I can combine Math.Round with PadLeft, or if there's something else wrong with what I'm doing.

    Can anyone enlighten me please?

    Brian

    Wednesday, March 11, 2009 3:29 PM

Answers

  • I think thats str function replacing 0 before period

    Math.Round(((5 - ms.mmoll(10)) / ms.ce), 1).ToString.PadLeft(3, "0"c)

    Arjun Paudel
    Wednesday, March 11, 2009 3:44 PM
  •         Dim d As Double = Math.PI 
            Dim ds As String 
            ds = Math.Round(d - 3, 1).ToString("#0.0"
     


    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
    • Proposed as answer by Blackwood Thursday, March 12, 2009 1:57 AM
    • Marked as answer by Martin Xie - MSFT Tuesday, March 17, 2009 3:56 AM
    Wednesday, March 11, 2009 8:35 PM

All replies

  • I think thats str function replacing 0 before period

    Math.Round(((5 - ms.mmoll(10)) / ms.ce), 1).ToString.PadLeft(3, "0"c)

    Arjun Paudel
    Wednesday, March 11, 2009 3:44 PM
  •         Dim d As Double = Math.PI 
            Dim ds As String 
            ds = Math.Round(d - 3, 1).ToString("#0.0"
     


    Looking for work - Zip 65101 http://www.vbforums.com/showthread.php?t=552774
    • Proposed as answer by Blackwood Thursday, March 12, 2009 1:57 AM
    • Marked as answer by Martin Xie - MSFT Tuesday, March 17, 2009 3:56 AM
    Wednesday, March 11, 2009 8:35 PM
  • I've been away and so couldn't mark this as answered. Thanks Arjun and dbasnett, these approaches worked fine.

    Lovely old job,

    Brian

    Thursday, March 26, 2009 11:36 AM