简体   繁体   English

Java类型转换/缩小原始变量

[英]Java type casting / narrowing of primitive variable

I'm getting exponential value in the result of expression where I'm adding double with long. 我在表达式的结果中得到指数值,在其中我将long加倍。

package com.testing;

import java.util.Date;

public class TypeCasting {

    public static void main(String[] args) {
        long varA = 100000;
        long varB = 3000000;

        double logVarA = Math.log10(varA);      //  5.0
        double logVarB = Math.log10(varB);      // 6.477121254719663

        long timeStampInSec = new Date().getTime() / 1000;
        System.out.println(timeStampInSec);     // 1552543503

        double totalValue = logVarA + logVarB + timeStampInSec;
        System.out.println(totalValue);         // 1.5525435144771214E9

        double finalScoreDampingFactor = 1000;

        double finalScore = totalValue / finalScoreDampingFactor;
        System.out.println(finalScore);         // 1552543.5144771214   
    }

}

In totalValue variable why I'm getting 1.5525435144771214E9 value and when I'm deviding it with 1000, getting 1552543.5144771214 . totalValue变量中,为什么我要获得1.5525435144771214E9值,而当我将其与1000 相乘时 ,就得到1552543.5144771214

Can any body please explain ? 可以请任何人解释吗?

A number has the same value, regardless of how it's represented. 无论数字如何表示,数字都具有相同的值。 What you see printed as a String is just a representation of the number. 您看到的显示为字符串的内容只是数字的表示。

The JVM has decided, for whichever reason, that the number should be printed in exponential form. 不论出于何种原因,JVM都决定以指数形式打印数字。 If you want to force it to print it differently, you can use printf . 如果要强制以其他方式打印它,可以使用printf

double totalValue = logVarA + logVarB + timeStampInSec;
System.out.printf("%f%n", totalValue);         // 1552547672.477121

1.5525435144771214E9 is 1552543514.... divided by 1000 is 1552543.... 1.5525435144771214E91552543514 ....除以1000是1552543 ....

Is it possible that you misinterpreted the 1.5525435144771214E9 ? 您是否可能误解了1.5525435144771214E9

The notation EX means "times 10 to the power of X". EX含义是“ X的10倍”。

So the original number was 1552543514.4771214 . 因此原始号码为1552543514.4771214

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM