Question percentage of time

Infinity

Member
Joined
Sep 7, 2009
Messages
6
Programming Experience
Beginner
hi guys i need to reduce an amount of time by x% this is what i have so far

VB.NET:
Public Sub timeconverter()
        '---------------------place hours minutes and seconds into variable-------------------------
        buildtimehours = Mid$(irontime.Text, 1, 2)
        buildtimeminutes = Mid$(irontime.Text, 4, 2)
        buildtimeseconds = Mid$(irontime.Text, 7, 2)
        '-------------------------------------------------------------------------------------------
        '-----------------------------break hours down into seconds---------------------------------
        buildtimehours = buildtimehours * 60 * 60
        buildtimeminutes = buildtimeminutes * 60
        buildtimetotal = buildtimehours + buildtimeminutes + buildtimeseconds
        '-------------------------------------------------------------------------------------------
        '---------------------------get construction level percent bonus----------------------------
        buildtimeconstructionlvl = constructionlvla.Value * 10
        buildtimebonus = buildtimetotal / 100
        buildtimepercent = buildtimebonus * buildtimeconstructionlvl
        buildtimetotal = buildtimetotal - buildtimepercent
        '-------------------------------------------------------------------------------------------
        '------------------convert the total back into hours minutes and seconds--------------------
        buildtimehoursback = buildtimetotal / 60
        buildtimehoursback = buildtimehoursback / 60
        buildtimehours = Mid$(buildtimehoursback, 1, 2)

        '-------------------------------------------------------------------------------------------
  


    End Sub

as u can see ive taken the hours minutes seconds from the time and stored each in a seperate variable so from 02:30:00 i then divided the hours by 60 and then 60 again to give me the total seconds in the hours and then divided the minutes by 60. i then added all 3 to give the total amount of seconds so now the buildtimetotal variable now holds the total in seconds i now need to reducce this by x% all vars are dimentioned as integers except for buildtimehoursback which is a double the problem seems to be when i try to convert the total seconds back to hours minutes seconds.
any help appreciated
 
You've made life quite hard for yourself, havent you :)

There are several easier ways to do this, one of which is below :-

VB.NET:
        Dim IronTime As String = "02:30:00"

        Dim BuildTime As TimeSpan
        Dim BuildTimeTotal As Integer
        Dim BuildTimeBack As TimeSpan

        If TimeSpan.TryParse(IronTime, BuildTime) = True Then
            BuildTimeTotal = Convert.ToInt32(BuildTime.TotalSeconds * 0.9)
            BuildTimeBack = TimeSpan.FromSeconds(BuildTimeTotal)
            MessageBox.Show(BuildTimeBack.ToString)
        End If
 
wow thanks that works perfectly
what would be the right way to code it to get x% of an integer
I can do this but as you can see my coding is not very good
thanx
 
There is no technically "right way", as any method that generates the correct result is fine. An example would be :-

VB.NET:
        Dim IntegerToUse As Integer = 3600
        Dim PercentageRequired As Double = 32.3
        Dim RequiredValue As Double = ((PercentageRequired / 100) * IntegerToUse)
 
Back
Top