Results 1 to 11 of 11

Thread: Problem regarding convertion of number...

  1. #1

    Thread Starter
    Registered User
    Join Date
    Dec 2013
    Posts
    1

    Post Problem regarding convertion of number...

    I m using visual basic 2010 express edition. Now I m having problem when it come to a big number such as 8700000000. I wan to convert tis number into 8.7 * 10 ^ 9. Please help me... I just start learning vb and dun knw much about it... Kindly reply me with a simply answer... TY

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Problem regarding convertion of number...

    That's not a maths problem. It's just a .NET string formatting problem. I have asked the mods to move this thread to the VB.NET forum.

    You can call ToString on a numeric value in VB to create a String representation of the number. You can pass a format string as an argument to format the output in a particular way, including scientific notation. Start reading here to learn what formats you can create:

    http://msdn.microsoft.com/en-us/library/427bttx3.aspx

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Problem regarding convertion of number...

    Thread moved to the VB.Net forum.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    Re: Problem regarding convertion of number...

    for the type of scientific notation you require, try this:

    Code:
    Private Function getScientificNotation(ByVal srcValue As Decimal) As String
        Dim power As Integer = 1
        If srcValue >= 1D Then
            Do While (srcValue / 10D) >= 1D
                srcValue /= 10D
                power += 1
            Loop
            Dim outPut As String = srcValue.ToString.TrimEnd("0"c, "."c)
            Return outPut & " * 10^" & (power - 1).ToString
        Else
            Do While (srcValue * 10D) <= 10D
                srcValue *= 10D
                power += 1
            Loop
            Dim outPut As String = srcValue.ToString.TrimEnd("0"c, "."c)
            Return outPut & " * 10^" & If(outPut.Contains("."), (-(power - 1)).ToString, (-(power - 2)).ToString)
        End If
    End Function

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Problem regarding convertion of number...

    Quote Originally Posted by .paul. View Post
    Code:
    Private Function getScientificNotation(ByVal srcValue As Decimal) As String
        Dim power As Integer = 1
        If srcValue >= 1D Then
            Do While (srcValue / 10D) >= 1D
                srcValue /= 10D
                power += 1
            Loop
            Dim outPut As String = srcValue.ToString.TrimEnd("0"c, "."c)
            Return outPut & " * 10^" & (power - 1).ToString
        Else
            Do While (srcValue * 10D) <= 10D
                srcValue *= 10D
                power += 1
            Loop
            Dim outPut As String = srcValue.ToString.TrimEnd("0"c, "."c)
            Return outPut & " * 10^" & If(outPut.Contains("."), (-(power - 1)).ToString, (-(power - 2)).ToString)
        End If
    End Function
    There's quite a lot wrong with that code. It didn't compile for me at first (needed .ToString() instead of .ToString? kinda weird), but more importantly it's inefficient, overcomplicated, and doesn't handle edge cases. The "If(...)" can be removed if "<= 10D" is changed to "< 10D", and the "Dim outPut ... / Return ..." lines can be deduplicated and put after the if block (with some minor changes to "power"'s indexing). The if block and loops can be completely avoided by using a logarithm. Also, it errors when passed negative values and hangs when passed 0.

    Here's a second draft:

    Code:
        Private Function getScientificNotation(ByVal srcValue As Decimal) As String
            If srcValue = 0 Then Return "0 * 10^0"
            
            Dim power As Integer = Math.Floor(Math.Log(Math.Abs(srcValue), 10))
            Dim outPut As String = (srcValue / 10^power).ToString.TrimEnd("0"c, "."c)
            Return outPut & " * 10^" & power.ToString
        End Function
    Even still, it'd definitely be preferable to use the built-in string format functions jmcilhinney suggested, though there doesn't seem to be one that exactly matches your use case. You could use the "E" format option and textually replace the "E" symbol with " * 10^"; that would at least be straightforward and easy to maintain. (Look at the custom string format entry to see how to handle leading 0's and such.)
    Last edited by jemidiah; Dec 16th, 2013 at 02:24 PM.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    Re: Problem regarding convertion of number...

    ok jemidiah, I stand corrected I can't argue with your very experienced mathematical abilities
    Can you explain how this works?:

    Code:
    Dim power As Integer = Math.Floor(Math.Log(Math.Abs(srcValue), 10))

  7. #7
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Problem regarding convertion of number...

    Sure. It was so straightforward to write that line but now that I'm writing an explanation it seems more involved. There must be a better way to say it, but oh well.

    Log(x, b) returns the number y such that b^y = x. It's essentially the opposite of exponentiating b. In this case, if x is srcValue and Abs(x) is its absolute value (i.e. throw away the negative sign, if there is one), then Log(Abs(x), 10) by definition satisfies 10^Log(Abs(x), 10) = Abs(x). The reason I've used Abs(x) instead of x is that Log isn't (at this level) defined for negative inputs. The reason is straightforward: there is no value of y where 10^y = -1, since 10^y will always be positive.

    Also by definition, Floor(y) is the integer that satisfies Floor(y) <= y < Floor(y)+1. More concretely, it just rounds y down. So, letting power = Floor(Log(Abs(x), 10)), we have Log(Abs(x), 10) = power + z for some value 0 <= z < 1. Hence,

    Abs(x) = 10^Log(Abs(x), 10)
    = 10^(power + z)
    = 10^power * 10^z

    Note that 1 = 10^0 <= 10^z < 10^1 = 10, so 10^z = Abs(x)/10^power is almost exactly what we want for the constant part of the scientific notation, and power is exactly what we want for the exponent. Indeed, we have

    x = 10^power * (+/- 10^z)
    = 10^power * (x/10^power)

    which is exactly what we want for scientific notation. It's unfortunate that this doesn't handle the x=0 case, but it's "even less possible" to define Log(0, 10) than Log(-1, 10), hence the opening "If" statement.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,466

    Re: Problem regarding convertion of number...

    Quote Originally Posted by jemidiah View Post
    (Log(x, b)) It's essentially the opposite of exponentiating b.
    that's the part I needed to know. I must've missed that day at school

  9. #9
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Problem regarding convertion of number...

    Of course, jemidiah's code is the correct and elegant way to go about it. But just playing with the options I came up with an alternative.

    Code:
        Private Function SN(ByVal decValue As Decimal) As String
            Dim strV As String = decValue.ToString
            If decValue < 0 Then
                Return strV.Substring(0, 2) - CDec("." & strV.Substring(2, strV.Length - 2) & "0") & "*10^" & strV.Length - 2
            Else
                Return strV.Substring(0, 1) + CDec("." & strV.Substring(1, strV.Length - 1) & "0") & "*10^" & strV.Length - 1
            End If
        End Function
    It only works if the original is an integer.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  10. #10
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Problem regarding convertion of number...

    or this. LOL

    Code:
        Private Function SN(ByVal decV As Decimal) As String
            Dim strV As String = decV.ToString
            Dim tmp As Integer = IIf(decV < 0, 2, 1)
            Return strV.Substring(0, tmp) - (tmp \ 2 + tmp - 2) * CDec("." & strV.Substring(tmp, strV.Length - tmp) & "0") & "*10^" & strV.Length - tmp
        End Function
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  11. #11
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: Problem regarding convertion of number...

    Hah, that... is disgusting. I would never want to run across it without having written it .
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width