转换分钟进入人类可读的格式

编程入门 行业动态 更新时间:2024-10-26 09:29:08
本文介绍了转换分钟进入人类可读的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

快速的问题。

有一个更聪明/更时尚的方式来分钟,转换成更易读的格式,只显示最显著的数字?

Is there a smarter/sleeker way to convert minutes into a more readable format, showing only the most significant digit?

我使用Android Studio中的Java。

I'm using Android Studio's Java.

public String MinutesToHumanReadable(Long minutes) { ... }

2 mins = "2 mins" 45 mins = "45 mins" 60 mins = ">1 hr" 85 mins = ">1 hr" 120 mins = ">2 hrs" 200 mins = ">3 hrs" 1500 mins = ">1 day"

我的code是非常繁琐的,不拘小节,而且有点无法读取。

My code is very cumbersome, sloppy, and somewhat unreadable.

public String MinutesToHumanReadable(long minutes) { String sReturn = ""; if (minutes > 515600) { sReturn = "> 1 yr"; } else if (minutes > 43200) { sReturn = (minutes / 43200) + " mths"; } else if (minutes > 10080) { sReturn = (minutes / 10080) + " wks"; } else if (minutes > 1440) { sReturn = (minutes / 1440) + " days"; } else if (minutes > 60) { sReturn = (minutes / 60) + " hrs"; } else { //<60 sReturn = minutes + " mins"; } return sReturn; }

非常感谢,Ĵ

Many thanks, J

推荐答案

那么它是可能的,我看着办吧,我为此而自豪! :)请注意,您可以轻松地只能由这两个数组改变数值添加任何其他值,而不改变方法本身。例如,如果年是不够的,你可以添加十年和世纪...

Well it is possible, I figure it out and I am proud of it! :) Note that you can easily add any other value without changing the method itself, only by changing values in these two arrays. For example, if "years" are not enough for you, you can add "decades" and "centuries"...

这code还增加了S字母结尾,如果你产值已超过1。

This code also adding "s" letter at the end, if you have more than 1 of output value.

public class SuperMinutesChangerClass { public static int[] barriers = {1, 60, 60*24, 60*24*7, 60*24*365, Integer.MAX_VALUE}; public static String[] text = {"min", "hr", "day", "week", "year"}; public static String minutesToHumanReadable(int minutes){ String toReturn = ""; for (int i = 1; i < barriers.length; i++) { if (minutes < barriers[i]){ int ammount = (minutes/barriers[i-1]); toReturn = ">" + (ammount) + " " + text[i-1]; if (ammount > 1){ toReturn += "s"; } break; } } return toReturn; } }

样品输入:

public static void main(String[] args) { System.out.println(minutesToHumanReadable(10)); System.out.println(minutesToHumanReadable(60)); System.out.println(minutesToHumanReadable(61)); System.out.println(minutesToHumanReadable(121)); System.out.println(minutesToHumanReadable(8887)); System.out.println(minutesToHumanReadable(9999743)); }

输出是:

>10 mins >1 hr >1 hr >2 hrs >6 days >19 years

更多推荐

转换分钟进入人类可读的格式

本文发布于:2023-11-11 06:33:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577643.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可读   人类   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!