加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java文字除以非常大的数字

发布时间:2020-12-15 04:20:50 所属栏目:Java 来源:网络整理
导读:我有一个代码,我需要将一个整数除以一个非常大的数字,例如12345678912.我使用了很长时间但它仍然给出了错误,因为int类型的文字12345678912超出了范围. 代码例如: public static void main(String[] args) { //rest of the code long x = 12345678912; //--e
我有一个代码,我需要将一个整数除以一个非常大的数字,例如12345678912.我使用了很长时间但它仍然给出了错误,因为int类型的文字12345678912超出了范围.
代码例如:

public static void main(String[] args) {

    //rest of the code 

    long x = 12345678912; //<--error is in this statement

    System.out.println(y/x); //<---y is an integer which is having some value in rest of the code.
}

我知道无符号长整数可以容纳的最大值是2 ^(64)-1.但是,我想知道,如何在Java中实现这一目标?有没有办法直接实现它,或者我需要实现任何算法?

解决方法

Java Language Specification

An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

你需要通过附加文字l或L来告诉数据类型很长

long x = 12345678912L;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读