长浮点数输出显示字母 [英] Long float-number output shows letters

查看:182
本文介绍了长浮点数输出显示字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

 字符串curDir =。; 
文件fileObject = new File(curDir);
File [] fileList = fileObject.listFiles();

float fileLengthMegabytes =(float)fileList [i] .length()/ 1000000;

方法fileList [i] .length()返回311字节,类型为Long。



前面的代码产生以下输出:

3.88E-4

如何在fileLengthMegabytes变量中获得期望的0,000311输出?

这是科学记数法。

你得到的是388而不是311,因为你除以1000000而不是1048576(1024 * 1024)



编辑:311即使1048576也没有实现,这样你得到370 ...所以错误可能是在你的calc;)

正如此处所述,您只需将科学记数法转换为十进制记数法一个格式化程序。

  DecimalFormat df = new DecimalFormat(#。########); 
return df.format(fileLengthMegabytes);

运行示例 http://ideone.com/2lkKv7

  import java .util *。 
import java.lang。*;
import java.text。*;
$ b $ class main
{
public static void main(String [] args)throws java.lang.Exception
{
DecimalFormat df = new DecimalFormat ###########);

float fileLengthMegabytes1 =(float)388/1000000;
float fileLengthMegabytes2 =(float)388/1048576;
System.out.println(科学记数法中的MB1:+
fileLengthMegabytes1);
System.out.println(十进制表示法中的MB1:+
df.format(fileLengthMegabytes1));
System.out.println(MB2 in Scientific Notation:+
fileLengthMegabytes2);
System.out.println(十进制表示法中的MB2:+
df.format(fileLengthMegabytes2));




$输出:


科学记数法中的MB1:3.88E-4

十进制记数法中的MB1:0.000388


使用科学计数法的MB2:3.7002563E-4

<

I have the following code:

String curDir = ".";
File fileObject = new File(curDir);
File[] fileList = fileObject.listFiles();

float fileLengthMegabytes = (float)fileList[i].length() / 1000000;

The method fileList[i].length() returns 311 bytes as the type Long.

The previous code results in the following output:

3.88E-4

How do I get my expected output of 0,000311 inside the fileLengthMegabytes variable?

解决方案

That is Scientific Notation.

AND you are getting 388 instead of 311 because you are dividing by 1000000 instead of 1048576 (1024 * 1024)

EDIT: 311 is not achieved even with 1048576, that way you get 370... so the error is probably in your calc ;)

As described here , you just have to convert your Scientific Notation to a Decimal Notation through a Formatter.

DecimalFormat df = new DecimalFormat("#.########");
return df.format(fileLengthMegabytes);

Running Example: http://ideone.com/2lkKv7

import java.util.*;
import java.lang.*;
import java.text.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
                DecimalFormat df = new DecimalFormat("#.##########");

        float fileLengthMegabytes1 = (float) 388 / 1000000;
        float fileLengthMegabytes2 = (float) 388 / 1048576;
        System.out.println("MB1 in Scientific Notation: " + 
                            fileLengthMegabytes1);        
        System.out.println("MB1 in Decimal Notation: " + 
                            df.format(fileLengthMegabytes1));
        System.out.println("MB2 in Scientific Notation: " + 
                            fileLengthMegabytes2);        
        System.out.println("MB2 in Decimal Notation: " + 
                            df.format(fileLengthMegabytes2));
        }
}

Output:

MB1 in Scientific Notation: 3.88E-4

MB1 in Decimal Notation: 0.000388

MB2 in Scientific Notation: 3.7002563E-4

MB2 in Decimal Notation: 0.0003700256

这篇关于长浮点数输出显示字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆