Converting rate charge formula to C expression - c

A call is charged 30 cents per minute.
The cost for line rental is RM60.00. The tax for the overall bill including the line rental) is 15%.
Calculate the amount, that needs to be paid by the user given the number of
minutes that the user uses his/her mobile phone.
How can I transform this formula into C code?
rate = (minute*0.30)+15/100 *60

The formula is wrong. You first need to multiply the number of minutes by 30 cents, then add the 60 for the rental, and only then apply the tax by multiplying by 1.15:
int minutes = // inputted from user...
double total = ((0.3 * minutes) + 60) * 1.15;

I think is just that:
float minute;
float rate;
printf("How many minutes?");
scanf("%f", &minute);
rate = (minute * 0.3 + 60) + 0.15 * (minute * 0.3 + 60);

Related

Converting seconds in date form Numpy Python

The delta_s function calculates the difference of time between 2 dates in the dates with seconds. Then the average median and max values for the differences is calculated. I am trying to convert the times in seconds for average median and max values but it does not work. i want to convert it in the form of x days x hours x minutes x seconds.
Code:
import numpy as np
dates= np.array(['2017-09-15 07:11:00' ,'2017-09-15 11:25:30', '2017-09-15 12:11:10', '2021-04-07 22:43:12', '2021-04-08 00:49:18'],
dtype="datetime64[ns]")
delta_s = np.diff(dates) // 1e9 # nanoseconds to seconds
delta_s = delta_s.astype(np.float64)
delta_avg = np.average(delta_s)
delta_median= np.median(delta_s)
delta_max = np.max(delta_s)
delta_max_index= np.argmax(delta_s)
The line delta_s = np.diff(dates) // 1e9 does not actually convert nanoseconds to seconds. It simply divides 1e9 to the timedelta object but the time unit is preserved timedelta64[ns].
>>> np.diff(dates)
array([ 15270000000000, 2740000000000, 112357922000000000,
7566000000000], dtype='timedelta64[ns]')
>>> np.diff(dates) // 1e9
array([ 15270, 2740, 112357922, 7566],
dtype='timedelta64[ns]')
This may mess up with any calculations you're doing.
Use
delta_s = np.array([np.timedelta64(td, 's') for td in np.diff(dates) ])
Currently there are no inbuilt functions to format timedelta to strings. However you can use something of this sort.
# Function to convert seconds to Human readable Timedelta string
def seconds_to_tdstring(total_seconds):
days, remainder = divmod(total_seconds, 60 * 60 * 24)
hours, remainder = divmod(remainder, 60 * 60)
minutes, seconds = divmod(remainder, 60)
return '{:02} Days {:02} Hours {:02} Minutes {:02} Seconds'.format(int(days), int(hours), int(minutes), int(seconds))
print(seconds_to_tdstring(delta_avg))
Output:
325 Days 04 Hours 24 Minutes 34 Seconds
I have modified the answer from a similar question .

Calculation of countdown timer

How numerators and denominators of days, hours and minutes are calculated in this code, why modulus is calculated in numerator?
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();
var x = setInterval(function() {
var now = new Date().getTime();
var distance = countDownDate - now;
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById("demo").innerHTML = days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
if (distance < 0) {
clearInterval(x);
document.getElementById("demo").innerHTML = "EXPIRED";
}
}, 1000);
Let me explain it line by line:
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();
In the above line, you are getting the milliseconds for the date Sep 5, 2018 15:37:25 from Jan 1, 1970 (which is the reference date being used by getTime()
var now = new Date().getTime();
var distance = countDownDate - now;
The above two lines are simple. now gets the current time in milliseconds and distance is the difference between the two times (also in milliseconds)
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
The total number of seconds in a day is 60 * 60 * 24 and if we want to get the milliseconds, we need to multiply it by 1000 so the number 1000 * 60 * 60 * 24 is the total number of milliseconds in a day. Dividing the difference (distance) by this number and discarding the values after the decimal, we get the number of days.
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
The above line is a little tricker as there are two operations. The first operation (%) is used to basically discard the part of the difference representing days (% returns the remainder of the division so the days portion of the difference is taken out.
In the next step (division), 1000 * 60 * 60 is the total number of milliseconds in an hour. So dividing the remainder of the difference by this number will give us the number of hours (and like before we discard the numbers after decimal)
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
This is similar to how hours are calculated. The first operation (%) takes out the hours portion from difference and the division (1000*60) returns the minutes (as 1000 * 60 is the number of milliseconds in a minute)
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
Here the first operation (%) takes out the minutes part and the second operation (division) returns the number of seconds.
Note: You might have noticed that in every operation the original distance is used but the code still works fine. Let me give you an example (I am using difference instead of distance as this name makes more sense).
difference = 93234543
days = Math.floor(89234543 / (1000 * 60 * 60 * 24))
=> days = 1
hours = Math.floor((89234543 % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
(result of modulus operation is 6834543, and division is )
=> hours = 1
This is a very important operation to understand:
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
distance(difference) / (1000 * 60 * 60) returns 25 (hours). As you can see we have already got 1 day and 1 hour (25 hours) so distance % (1000 * 60 * 60) wipes out all of these 25 hours and then the division calculates the minutes and so on.

Get the number of days of the current date with Time

I'm trying to get the number of days untill today. Like:
int days, seconds;
seconds = Time(0); // Get the number of SECONDS from January, 1º 1970 untill now.
days = seconds / (60 * 60 * 24);
printf("%d", days);
The output is: 16326.
But when I use some website that makes the conversion for you, they show 6464 days instead.
What am I doing wrong ?
You are right. There are 30 + 14 = 44 years, which gives about 16000 days.

Convert radians to degrees, minutes, and seconds

I am looking on a way to convert decimals to degrees in C. For instance, the asin() function in C returns a decimal number but I need that number to be in degrees ° minutes ' seconds ".
e.g. 1.5 would be 1°30'0"
The asin function returns radians. There are 2 π radians in a circle.
There are 360 degrees in a circle, 60 minutes in a degree, and 60 seconds in a minute. So there are 360*60*60 seconds in a circle.
double radians = asin(opposite / hypotenuse);
int totalSeconds = (int)round(radians * 360 * 60 * 60 / (2 * M_PI));
int seconds = totalSeconds % 60;
int minutes = (totalSeconds / 60) % 60;
int degrees = totalSeconds / (60 * 60);
Not sure how to do this with one command like >dms on the ti84, but you can use logic.
The whole units of degrees will remain the same (i.e. in 121.135°
longitude, start with 121°).
Multiply the decimal by 60 (i.e. .135 * 60 = 8.1).
The whole number becomes the minutes (8').
Take the remaining decimal and multiply by 60. (i.e. .1 * 60 = 6).
The resulting number becomes the seconds (6"). Seconds can remain as a
decimal.
Take your three sets of numbers and put them together, using
the symbols for degrees (°), minutes (‘), and seconds (") (i.e.
121°8'6" longitude)
Source:
http://geography.about.com/library/howto/htdegrees.htm
A little bit of searching and i found this in c#:
Converting from Decimal degrees to Degrees Minutes Seconds tenths.
double decimal_degrees;
// set decimal_degrees value here
double minutes = (decimal_degrees - Math.Floor(decimal_degrees)) * 60.0;
double seconds = (minutes - Math.Floor(minutes)) * 60.0;
double tenths = (seconds - Math.Floor(seconds)) * 10.0;
// get rid of fractional part
minutes = Math.Floor(minutes);
seconds = Math.Floor(seconds);
tenths = Math.Floor(tenths);
But as he said, it will need to be convered from radians to degrees first.

Decimal to HH:MM Conversion

I have a table and I want to update the Duration column which of datatype Decimal to HH:MM format.
ECode Duration
101 101.75
101 69.56
102 54.60
103 54.97
The output should be like this:
ECode Duration
101 102.15
101 69.56
102 55
103 55.37
We are calculating the time after the decimal and if after decimal 60 or more then 60 will be there we are adding 1 before decimal and remaining after subtraction from 60 we are showing as it is.
Example 101.75, here after decimal 75 is there so firstly we check it is greater than or equal to 60. If yes then subtract 60 and add 1 before decimal and put the remaining 15 after decimal. So the result should be 102.15.
Please share query if possible to solve this issue.
This should do it:
update thistable
set duration = duration + 0.4
where duration - floor(duration) >= 0.6
update YourTable set
Duration = floor(Duration) +
cast(((Duration - floor(Duration))*100) as int) / 60 +
cast((cast((Duration - floor(Duration)) * 100 as int) % 60) as float) / 100
I guess you would do something like ("pseudo" code of some sort):
String reorder(String x) {
String parts[] = x.split(.);
if(parts.length < 2) x;
// eval as integers:
if(parts[1] < 60) return x;
if(parts[1] > 60) return (parts[0]+(parts[1]/60))+"."+(parts[1]%60);
return parts[0]+1;
}
But exactly how you do it depends whether you're doing in a script, some application and the language you're using...

Resources