Solved

# Convert an Integer into percent

Posted on 2008-11-19
1,195 Views
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)
``````
0
Question by:cannase

LVL 59

Expert Comment

You can try the String.Format("{1:P}", 1000 * MyDatabaseValue).
0

LVL 59

Expert Comment

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)
0

LVL 59

Expert Comment

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
0

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
Else
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
0

Author Closing Comment

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

LVL 59

Expert Comment

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.
0

## Featured Post

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…