我有一个代表时间的数字,我需要将此数字转换为字符串。
如n = 800,表示时间= 8:00
目前我这样做:
n = 800;
string time = '' + n/100 + ' : ' + n % 100 ;
但是time
变为8 : 0
,但我希望以两位数格式分钟,例如8 : 00
有人请帮助我吗?
答案 0 :(得分:2)
var n = 800;
var hours = Math.floor(n/100);
var minutes = ('0' + (n%100)).slice(-2);
var time = hours + ':' + minutes;
请注意小时数的四舍五入,否则你最终会得到类似“8.56:56”的内容。
答案 1 :(得分:1)
如果你要在这里做的只是在最后两位数之前插入一个冒号,你可以做一个字符串替换:
Object<Is...>
或者将小时和分钟切片并用冒号连接:
#include <iostream>
#include <type_traits>
template <int...> class A;
template <typename...> struct Pack;
template <int...> struct P;
template <int N, int Count, typename Front, typename Output> struct GenerateHelper;
template <int N, int Count, int... Is, typename... As>
struct GenerateHelper<N, Count, P<Is...>, Pack<As...>> :
GenerateHelper<N, Count + 1, P<Is...>, Pack<As..., A<Is..., Count>>> {};
template <int N, int... Is, typename... As>
struct GenerateHelper<N, N, P<Is...>, Pack<As...>> {
using type = Pack<As...>;
};
template <typename...> struct Generate;
// Simple special case just to start off. Generate has only one pack to deal with.
template <int N, int... Is>
struct Generate<P<N, Is...>> : GenerateHelper<N, 0, P<Is...>, Pack<>> {};
int main() {
using T = Generate<P<3,0,0>>::type;
std::cout << std::is_same<T, Pack<A<0,0,0>, A<0,0,1>, A<0,0,2>>>::value << '\n'; // true
}
答案 2 :(得分:0)
只是一个捷径解决方案:P
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.