Retrieve decimals only from a variable - string

I'm making a distance/time used calculator where i'm trying to separate whole hours and minutes.
Code:
if (hours >= 1)
{
minutes = Number((hours-int.(hours))*60);
timeTxt.text = String(int.(hours)+" hours & "+(minutes)+" minutes");
}
else
{
timeTxt.text = String((hours*60).toFixed(0)+" minutes");
}
the "else" one is working and prints minutes only. But I can't get the first if statement to work. Trying to print "x hours & x minutes".
Getting this message in output:
TypeError: Error #1123: Filter operator not supported on type class int.
at Rutekalkulator2_fla::MainTimeline/calculate()

Couldn't you do something like:
(pseudocode)
double tmp = math.floor(yournumber);
yournumber = yournumber - tmp;
And that'll make your number be just the decimal part.
Besides that, I'm not really sure what your question is.

Convert everything to minutes then try the following:
timeTxt.text = String( totMinutes / 60 + ":" + totMinutes % 60);
// output 06:36
timeTxt.text = String( totMinutes / 60 + " hours and " + totMinutes % 60 + " minutes");
// output 6 hours and 36 minutes
I'm not fluent in Actionscript but if the modulo works the same as it does in other languages this should work for you.

Related

Groovy not converting seconds to hours properly

I have an integer that is:
19045800
I tried different code:
def c = Calendar.instance
c.clear()
c.set(Calendar.SECOND, 19045800)
echo c.format('HH:mm:ss').toString()
String timestamp = new GregorianCalendar( 0, 0, 0, 0, 0, 19045800, 0 ).time.format( 'HH:mm:ss' )
echo timestamp
Both return 10:30:00
19045800 seconds is supposed to be over 5000 hours. What am I doing wrong?
I'm not sure what you are looking for. But if your requirement is to calculate the number of hours, minutes, and the remainder of seconds for given seconds following code will work.
def timeInSeconds = 19045800
int hours = timeInSeconds/3600
int minutes = (timeInSeconds%3600)/60
int seconds = ((timeInSeconds%3600)%60)
println("Hours: " + hours)
println("Minutes: " + minutes)
println("Seconds: " + seconds)
You're attempting to use a Calendar, but what you're actually discussing is what Java calls a Duration – a length of time in a particular measurement unit.
import java.time.*
def dur = Duration.ofSeconds(19045800)
def hours = dur.toHours()
def minutes = dur.minusHours(hours).toMinutes()
def seconds = dur.minusHours(hours).minusMinutes(minutes).toSeconds()
println "hours = ${hours}"
println "minutes = ${minutes}"
println "seconds = ${seconds}"
prints:
hours = 5290
minutes = 30
seconds = 0

Why doesn't my if-statements return the right numbers?

First of all, this is my first week trying out C# or any other programming language for that matter, also my first post here on Stackoverflow!
Been working on this change calculator for a while now, trying to get it to round the result to either 0 if its <.25, 0.50 if it's between .25 and .75 and 1 if it's >.75. Seems like it's ignoring my if-statements, And on top of that the result I get isn't correct either. Some calculations ends up being negative, which I can't figure out why :/
double summa0 = vara - kontant; //item - change
var extrakt = (int)summa0; //removes decimals out of summa0 = 107
var avrundsSumma = summa0 - extrakt; //<--- extracts the decimals out of summa0
if (avrundsSumma < 0.25f)
{
avrundsSumma = Math.Floor(avrundsSumma);
}
else if (avrundsSumma > 0.75f) //Runs the decimals through if-statements
{
avrundsSumma = Math.Ceiling(avrundsSumma);
}
else
{
avrundsSumma = 0.5;
} // = in this case the result should be 1
double summa = extrakt + avrundsSumma; // 107 + 1 = 108
double attBetala = kontant - summa; // 500 - 108 = 392
Since I'm very new to this it's hard to know exactly which part of the code is causing the issue. When I run the code in CMD I get a negative result from "double summa = extrakt + avrundsSumma; // 107 + 1 = 108"
So instead of 108 I get -108.
Not sure what you mean by "Hard code the values" either :o

NPM library: Converting Milliseconds to Timecode help needed?

Can someone help with a quick script that will convert Milliseconds to Timecode (HH:MM:SS:Frame) with this NPM library?
https://www.npmjs.com/package/ms-to-timecode
I just need to pass a miliseconds number (ie 7036.112) to it and output the converted timecode. I've installed npm and the ms-to-timecode library on my debian server. I know perl/bash, and never coded with these library modules.
Any help is greatly appreciated.
-Akak
You need to write a javascript code to use this npm module.
const msToTimecode = require('ms-to-timecode');
const ms = 6000;
const fps = 30
const result = msToTimecode(ms, fps)
console.log(result) // Print 00:00:06:00
If you are using the npm module ms-to-timecode then:
const msToTimecode = require('ms-to-timecode');
const timeCode = msToTimecode(7036, 112);
console.log(timeCode); // displays 00:00:07:04
If you don't want to use the npm module then:
function msToTimeCode(ms, fps) {
let frames = parseInt((ms/1000 * fps) % fps)
let seconds = parseInt((ms/1000)%60)
let minutes = parseInt((ms/(1000*60))%60)
let hours = parseInt((ms/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
frames = (frames < 10) ? "0" + frames : frames;
return hours + ":" + minutes + ":" + seconds + ":" + frames;
};
console.log(msToTimeCode(7036, 112)); // displays 00:00:07:04

How to convert 24 hour format to 12 hour format in groovy [duplicate]

This question already has answers here:
How to convert 24 hr format time in to 12 hr Format?
(16 answers)
Closed 3 years ago.
I'm new to groovy and i want to convert 24 hour format to 12 hour format. What is the code to be used for it? Is there any built-in methods?
I just want groovy code not java one
Kevin's answer is correct, and should get the tick... I only post this as it's slightly shorter
import java.time.*
import static java.time.format.DateTimeFormatter.ofPattern
String time = '13:23:45'
String result = LocalTime.parse(time).format(ofPattern('h:mm:ss a'))
println result
I thought this question is somewhat similar to the How to convert 24 hr format time in to 12 hr Format?. It just that Java and Groovy share a lot of similarities. To point that out, I took Cloud's answer from the mentioned question and converted that to Groovy.
import java.time.LocalTime
import java.time.format.DateTimeFormatter
final String time = "21:49"
String result = LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm")).format(DateTimeFormatter.ofPattern("hh:mm a"));
println(result)
If you want to build your own time function you can try to customize the code below.
final String time = "21:49"
final String _24to12( final String input ){
if ( input.indexOf(":") == -1 )
throw ("")
final String []temp = input.split(":")
if ( temp.size() != 2 )
throw ("") // Add your throw code
// This does not support time string with seconds
int h = temp[0] as int // if h or m is not a number then exception
int m = temp[1] as int // java.lang.NumberFormatException will be raised
// that can be cached or just terminate the program
String dn
if ( h < 0 || h > 23 )
throw("") // add your own throw code
// hour can't be less than 0 or larger than 24
if ( m < 0 || m > 59 )
throw("") // add your own throw code
// minutes can't be less than 0 or larger than 60
if ( h == 0 ){
h = 12
dn = "AM"
} else if ( h == 12 ) {
dn = "PM"
} else if ( h > 12 ) {
h = h - 12
dn = "PM"
} else {
dn = "AM"
}
return h.toString() + ":" + m.toString() + " " + dn.toString()
}
println(_24to12(time))

Distribute a value value along time interval in array. I am using nodejs.

Sorry if the text is confusing, I don't speak English.
My problem is:
1. I have the number of pages that was printed.
2. The duration of printing (start time and finish)
3. I want to plot a chart by hour whith the number of pages per hour
Example:
900 pages
1:30 hous
I want this Array of hour: [600, 300]
I think this is more a mathematical problem, but I don't had a good idea to do this. The are a lot of data and i need to do a algorithm fast and optimized.
Obs: I am more interested in the logic, not in the programming language.
Ok, not sure that works for everything but I think is a good start.
I have made the assumption that your duration is in minutes or else I believe you can transform it to minutes.
function something(pages,totalDutation){
// pages = 900
// totalDutation = 90
var printsPerMinute = pages / totalDutation //get the prints per minute!
var printsPerHour = Math.floor(printsPerMinute * 60) //calculate the prints made in one hour
var countOfHours = parseInt(totalDutation / 60) //divide the total duration by 60 to get the count of hours
var remainingPrints = (totalDutation % 60) * printsPerMinute //add the extra prints that didn't complete a whole hour.
var result = Array(countOfHours).fill(printsPerHour) //create an array and fill it.
if(remainingPrints) result.push(remainingPrints)
return result
}
Take a look at the NodeJS example below to get an idea of how it can be done.
Might need a bit of fine-tuning, though.
Calculate the number of prints per hour
Make as many iterations as hours taken
Do (total prints - prints per hour) as long as a > b,
otherwise return the remaining total prints
const getPagesPerHour = function (totalPrints, totalTime) {
const printsPerHour = (totalPrints / totalTime) * 60;
return Array.apply(null, { length: Math.ceil(totalPrints/printsPerHour) }).map(function (val, key) {
if (totalPrints > printsPerHour) {
totalPrints = (totalPrints - printsPerHour);
return printsPerHour;
} else {
return totalPrints;
}
});
}
console.log(getPagesPerHour(900, 90)); // [600, 300]

Resources