• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1231
  • Last Modified:

Convert an Integer into percent

I have an asp.net 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

  • 4
1 Solution
Kevin CrossChief Technology OfficerCommented:
You can try the String.Format("{1:P}", 1000 * MyDatabaseValue).
Kevin CrossChief Technology OfficerCommented:
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)
Kevin CrossChief Technology OfficerCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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
cannaseAuthor Commented:
thank you - This worked to convert 10 to 0.1 and the calculation worked correctly
Kevin CrossChief Technology OfficerCommented:
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.  

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now