从 BigDecimal 值中删除毫秒

Cut the Milliseconds from the BigDecimal value

我在数据库中有以下值“4.6144444444”作为 BigDecimal,它在时间模式上符合“04:36:51”。

4.6144444444* 3600 0000 = 16611999.99984/1000 = 16611.99999984 秒 = 276.866666664 分钟。

这个值 0.866666664*60 = 51.99999984 秒也总的是'04:36:51.99999984'。有没有办法从'4.6144444444'中削减'0.99999984'的时间值?

因为我在 Jasper Report 中将这些值相加来计算总和。

0.1197222222 + 4.6144444444 + 0.7480555555 + 0.9475000000 = 6.4297222221 ==> 06:25:46 999 999 56

00:07:10 + 04:36:51 + 00:44:52 + 00:56:51 = 06:25:44

结果也有两秒的差异。有没有办法在 Java 或 Jasper 中管理它?

代码

        def duration = rowTemp[7];
        def durationMiliseconds = duration * 3600000;
        def durationSeconds = durationMiliseconds/1000;
        def durationMinutes = (durationSeconds/60).toString();

        String[] durationMinutesSplitt = durationMinutes.split(".");
        def secondsPart = Double.parseDouble("0." + durationMinutesSplitt[1]);
        def secondWithMiliSeconds = secondsPart * 60;

在贾斯珀

在 jasper 中,我将 vlaue 渲染如下:

        <textField isStretchWithOverflow="true" isBlankWhenNull="true">
            <reportElement stretchType="RelativeToTallestObject" x="252" y="2" width="149" height="18" />
            <box topPadding="2" leftPadding="2" bottomPadding="2" rightPadding="2"/>
            <textElement textAlignment="Right" verticalAlignment="Middle">
                <font size="8" isBold="true" pdfFontName="Helvetica-Bold"/>
                <paragraph lineSpacing="Single"/>
            </textElement>
            <textFieldExpression><![CDATA[org.apache.commons.lang.time.DurationFormatUtils.formatDuration((long)$V{group1DurationStay}.doubleValue() * 3600000, "HH:mm:ss")]]></textFieldExpression>
        </textField>

所以浮动很难,因为它们不精确,但好吧,浮动。可以在例如上找到一个很好的解释。维基百科 https://en.wikipedia.org/wiki/Floating-point_arithmetic

在您的情况下,您会直觉地希望从初始数字中删除 0.99999984/3600=0.00027777773333333334,从而得到 4.614166666666667。但这将导致 51.000000000001364 (因为浮动的工作方式)。

如果只在乎最后的结果,那就考虑

def duration = rowTemp[7];
def durationSeconds = duration*3600;
def durationMinutes = (durationSeconds/60);
def secondsPart = durationMinutes - floor(durationMinutes)
def secondWithMiliSeconds = secondsPart * 60;
def secondWithoutMiliSeconds = floor(secondsPart * 60);

(代码未经测试)