i want a timer that doesn't count every second. i want it to run slower
timer = timer + Time.deltaTime;
int itimer = (int)timer;
int minutes = itimer % 60;
int hours = itimer / 60;
string time = hours.ToString() + ":" + minutes.ToString().PadLeft(2, '0');
this is the code now. the clock currently starts at 19:20 and i want the minute counter to go up every 4 seconds or something(i still have to figure the exact timing out). i tried doing "Time.deltaTime*0.9", but the code doesn't work like that. how can i best slow it down with this code? also, when it's 20:00 (or 1200 before conversion) i'd like for something to happen, so i still need access to that number
thank you
Likely the problem you had was a casting error when you tried assigning the result back to timer. This is because by default when you did 0.9 it did it as a double and your variable timer was a float. Add a f to the end of the number to mark it a float and it should work.
timer = timer + Time.deltaTime * 0.9f;
int itimer = (int)timer;
int minutes = itimer % 60;
int hours = itimer / 60;
string time = hours.ToString() + ":" + minutes.ToString().PadLeft(2, '0');
Related
I have a progress that advances according to three operation.
The first operation takes 10 seconds, the second operation takes 15 seconds and the third operation takes 15 seconds.
The progress percentage can be calculated using this formula:
elapsedTime/totalTime * 100%
where totalTime is 10+15+15 = 40 seconds.
Suppose now that each operation can have an error that is calculated at the end of each operation (for example the first operation takes 3 seconds more, so the time to complete it is 13 seconds).
What is the new way to recalculate the progress percentage so that the progress
always goes forward (it should proceed slowly but never go backwards)?
The Maximum of my progress is set to 100.
what if you added the error_Time to both sides? I mean something like this:
(op1+ error_Time_op1 + op2 +op3 )/(totalTime + error_Time_op1 ) * 100%
increase the totalTime by the amount that the error has added to the overall progress
totalTime + error_time
for the progress to go always forward you should freeze the display at the value when the error happens and only proceed to update when the newly_calculated percentage exceeds the remembered value.
image you are at the end of operation 2:
totalTime = 40;
elapsedTime = 25;
old_progress = 25 / 40 * 100 [62.5%]
then the error occurs:
errorTime = 3;
totalTime = 40 + errorTime ;
elapsedTime = 25;
new_progress = 25 / 43 * 100 [58.1%]
now you wait until the new_progress value exceeds the old_progess value and then you can update again. This way it will never go backwards
I have a user input values in my table.
Here if user enters 3.2, it mean 3 hours and 20 min.
I am showing the total hours that has been input by the user in the entire week.
Inputs are:
Sun : 3.2
Mon : 4.5
Tue: 5.0
Now, 3.2 + 4.5 + 5.0 = 12.70 which would mean 12 hours and 70 min.
However, I want the result to be 13.10 (which is 13 hours and 10 min) instead of 12.70.
I need the total in a select query which is binding my grid with the rest of the data.
Currently i am using the sum function in the select query along with other columns.
How do I do this?
Your input format won't work at all.
You are lucky it does in your example but in most cases it just won't. For instance, if 2 persons input "1 hour and 50 minutes" :
1.5 + 1.5 = 3.0
You cannot read it as : "three hours" since in reality it is "3 hours and 40 minutes".
As soon as the sum of "minutes" is greater thant 0.99, your are wrong.
But in the few lucky cases, you can do some arithmetic (if you want a result in the same "double" format as your input)?
var inputList = new List<double>() {3.2, 4.5, 5.0};
double total = inputList.Sum();
int baseHours = (int)Math.Floor(total);
int realBaseHours = (int)inputList.Sum(d => Math.Floor(d));
if (baseHours > realBaseHours)
throw new Exception("All hell breaks loose!");
int baseMinutes = (int)(total * 100.0 - baseHours * 100.0);
int finalHours = baseHours + baseMinutes / 60;
int finalMinutes = baseMinutes % 60;
double result = finalHours + finalMinutes / 100.0;
It's not good to saving times as a double format, but for your question:
Get all times as array of double values and do some arithmetic:
double[] times = { 3.2, 4.5, 5.0 };
int hours = 0;
int minuts = 0;
string[] values;
foreach (double t in times)
{
values = t.ToString("#.00").Split('.');
hours += int.Parse(values[0]);
minuts += int.Parse(values[1]);
}
hours += minuts / 60;
minuts += minuts % 60;
It work for all kind of times as double format.
This question already has answers here:
Allow only numbers to be inserted & transform hours to minutes to calculate gold/min - UPDATED
(2 answers)
Closed 9 years ago.
I've got stuck in my program, i need to calculate the gold/minute but my math formula won't do the desire thing. As I input the hours into a float(something like 1.2 hours) the transformation will be 72 min instead of 80 as I need.
Can you please help me ? I marked in comment below where the problem is.
And here is my code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace YourGold
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to YourGold App! \n------------------------");
Console.WriteLine("Inesrt your gold: ");
int gold = int.Parse(Console.ReadLine());
Console.WriteLine("Your gold is : " + gold);
Console.WriteLine("Inesrt your time(In Hours) played: ");
float hours = float.Parse(Console.ReadLine());
int minutes = 60;
float time = (float)hours * minutes; // Here the calculation are wrong...
Console.WriteLine("Your total time playd is : " + time + " minutes");
float goldMin = gold / time;
Console.WriteLine("Your gold per minute is : " + goldMin);
Console.WriteLine("The application has ended, press any key to end this app. \nThank you for using it.");
Console.ReadLine();
}
}
}
Thanks a lot.
P.S it's related to this question:Allow only numbers to be inserted & transform hours to minutes to calculate gold/min - UPDATED , I update it same as this but i think i should have done a new question as i did now(I'm still learning how to go on with this platform:) )
Use the built-in TimeSpan:
TimeSpan time = TimeSpan.FromHours(1.2);
double minutes = time.TotalMinutes;
TimeSpan.FromHours Method Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.
You can also do:
// string timeAsString = "1:20";
TimeSpan time;
if (TimeSpan.TryParse(timeAsString, CultureInfo.InvariantCulture, out time))
{
double minutes = time.TotalMinutes;
//... continue
}
else
{
// Ask user to input time in correct format
}
Or:
var time = new TimeSpan(0, 1, 20, 0);
double minutes = time.TotalMinutes;
If you really want your program to behave as you want do this.
time = (int)hours * 60 + (hours%1)*100
var minutes = TimeSpan.FromHours(1.2).TotalMinutes; // returns 72.0
var hours = 1.2;
var minutes = ((int)hours) * 60 + (hours%1)*100;
And a side note: such way of inputting time is IMO not a good one. It'll be confusing and I guess that more often than not people will be actually entering 1:20 instead of 1.2, which'll break your application. And if not, they might be entering 1.5 thinking of 90 minutes. I know I would have done it like that.
I am trying to make a timer in C#. I do this by counting frames then making a variable(seconds) that is equal to the number of frames divided by 60. I also count minutes by having another variable(minutes) that is equal to seconds divided by 60.
The problem
When minutes is upped by one, seconds keeps counting and i am not sure how to fix this. I want to add one to minutes whenever seconds reaches 60 but then go back down to zero without seting minutes to zero.
Here is what I've tried:
int frames = 0;
int seconds = 0;
int minutes = 0;
This is in update:
protected override void Update(GameTime gameTime)
{
minutes = seconds / 60;
seconds = frames / 60;
frames += 1;
I would suggest you just count frames, and derive seconds and minutes from that, whenever you need to. So if you really need to update all the variables in each Update call, you'd have:
frames++;
seconds = (frames / 60) % 60;
minutes = frames / (60 * 60);
Or to be clearer, define some constants:
const int FramesPerSecond = 60;
const int SecondsPerMinute = 60;
const int FramesPerMinute = FramesPerSecond * SecondsPerMinute;
...
frames++;
seconds = (frames / FramesPerSecond) % SecondsPerMinute;
minutes = frames / FramesPerMinute;
(If you need to reset minutes to 0 after an hour, you would need to extend this further.)
It's not clear what you're trying to do though - there may well be a better way of achieving it. If you're definitely trying to count frames, this is fine... but if you're trying to compute elapsed time, you should remember a base time and then subtract that from the current time, rather than relying on a timer firing exactly once per second.
Attention: If you count from 0 to 60 (for minutes and second) you count 61 seconds. So go from 0 to 59 in your conditions.
for example there are 5 fingers at a hand but if we count from 0 we have 6 finger. (are we mutand?)
:)
So, if you don't need System.Threading.Timer or System.Threading.DispatcherTimer, you can just use standard TimeSpan and add miliseconds to it.
Or create your own TimeSpan-like class with automatically incrementing properties.
I need to calculate the time difference faken for division most accurately in nano seconds. Please tell me to do this.
At Present i'm using a lower accuracy method in which the problem is that : when the first calculation is performed it shows 87 milliseconds or 65 milliseconds as answer. But when the function is called again second time or more, it only show 0 milliseconds.
The code is :
long startTick = DateTime.Now.Ticks;
double result = (double)22 / 7;
result = System.Math.Round(result, digit);
long endTick = DateTime.Now.Ticks;
long tick = endTick - startTick;
double milliseconds = tick / TimeSpan.TicksPerMillisecond;
time.Text = result + "\nThe division took " + milliseconds + " milliseconds to complete.";
digit is the parameter of function which is variable. No matter what the value of digit is the milliseconds value remains 0 after first calling of function....
Please suggest more accurate way in which calling the same function with different decimal digits will result in different time interval in c# for windows Phone.
I think the memory flush should be done before and after each calculation. But i dont know how to do this.
I don't like this tick method personally for accuracy. I've tried stopwatch also but its not working. Please suggest another method best suited in my case. I want result like : 0.0345 or 0.0714 seconds.
Thanks
You are performing integer division on this line:
double milliseconds = tick / TimeSpan.TicksPerMillisecond;
Even though you are declaring it as a double, a long divided by a long will truncate the decimal. You are better off doing:
double milliseconds = (double)tick / TimeSpan.TicksPerMillisecond;
Or better yet, just ditch the tick stuff all together:
DateTime start = DateTime.Now;
double result = (double)22 / 7;
result = System.Math.Round(result, digit);
DateTime end = DateTime.Now;
double milliseconds = (end - start).TotalMilliseconds;
time.Text = result + "\nThe division took " + milliseconds + " milliseconds to complete.";
You won't be able to get micro or nano level precision, but you will get millisecond precision with a margin of error.
You still may get zero, however. You are trying to time how long a simple division operation takes. You could do millions of division operations in less than a second. You may want to do it 1,000,000 times, then divide the result by a 1,000,000:
DateTime start = DateTime.Now;
for (var i = 0; i < 1000000; i++)
{
double result = (double)22 / 7;
result = System.Math.Round(result, digit);
}
DateTime end = DateTime.Now;
double milliseconds = (end - start).TotalMilliseconds / 1000000;
This still won't be completely realistic, but should get you an actual number.
Since you have the time in ticks, just increase the resolution by multiplying the denominator:
double microseconds = tick / (TimeSpan.TicksPerMillisecond * 1000.0);
Why are you not using StopWatch Class to do your time calulation.
It is meant to the calculate the time the you want ..
Here is a link for your reference.
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx
//if you want to get the full milliseconds you could also do something like this.
dateStartTime = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());
//then where you end the code do this
dateEndTime = Convert.ToDateTime(DateTime.Now.TimeOfDay.ToString());
ddateDuration = (TimeSpan)(dateEndTime - dateStartTime);
then to display out what you are actually looking for in terms of miliseconds do
Console.WriteLine(ddateDuration.ToString().Substring(0, 8));
// or some other method that you are using to display the results