Convert an Integer into percent

Posted on 2008-11-19
Last Modified: 2012-05-05
I have an 2.0 application.  A value of 10 is stored in our SQL database to represent a percentage we charge a client.  10 is being passed to our application as an integer.  
My question is - how would I convert the datafield value of 10 to 0.1 to be used in a mathmatical routine? or
How would I convert the the datafield value of 5 to 0.05 .....
Assume my variable is MyDatabaseValue which is 10

Revenue.Text = String.Format("{0:c}", 1000 * MyDatabaseValue)

Open in new window

Question by:cannase
    LVL 59

    Expert Comment

    by:Kevin Cross
    You can try the String.Format("{1:P}", 1000 * MyDatabaseValue).
    LVL 59

    Expert Comment

    by:Kevin Cross
    Sorry I see now what you are asking.  The {1:P} means to make the number a percentage, but you are wanting a currency amount times a percentage.

    Just do this:

    Revenue.Text = String.Format("{0:c}", 1000 * MyDatabaseValue / 100)
    LVL 59

    Expert Comment

    by:Kevin Cross
    You can also make sure it comes out of the DB the way you want it by doing this:

    SELECT CAST(datafieldvalue * 1.0/100 AS Money) AS percentage
    FROM TableName
    LVL 2

    Accepted Solution


    Change the value in the business-layer OR in the data-layer:

    Revenue.Text = String.Format("{0:c}", 1000 * (MyDatabaseValue / 100))

    Or create a function for it
    Private Function ToPct(nValue as object) as Double
        If nValue Is dbNull.value Then
          Return 0
        ElseIf Not IsNumeric(nValue) Then
          Return 0 'Or throw an error
          Return nValue / 100
        End If
    End Function
    Revenue.Text = String.Format("{0:c}", 1000 * ToPct(MyDatabaseValue))

    - Data-layer:
    Change your SELECT statement to something like this:
    SELECT CONVERT(float, MyDatabaseValue)/100 AS MyDatabasePercent FROM tablename

    Author Closing Comment

    thank you - This worked to convert 10 to 0.1 and the calculation worked correctly
    LVL 59

    Expert Comment

    by:Kevin Cross
    Glad you found what you were looking for.

    Given I gave same answers 30 minutes ago, please advise what made mine incorrect so I can better answer this question for others in the future.  Just curious.  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now