简体   繁体   English

将double(原始)转换为Long(包装器)

[英]Casting double (primitive) to Long ( Wrapper )

I have to cast primitive double to Long ( Wrapper) at multiple locations in my code. 我必须在代码中的多个位置将原始双精度类型转换为Long(Wrapper)。 These are 2 ways I could work with - 这是我可以使用的2种方法-

linkcatch.setCode(Long.valueOf((long) relatedCd.getDouble(value)));

( casting primitive double to primitive long and then Wrapper Long) (将原始double转换为long,然后包装长)

linkcatch.setCode(Long.valueOf(String.valueOf(relatedCd.getDouble(value))));

( casting double to String to Wrapper Long ) (将Double转换为String到Wrapper Long)

Is there any way to cast from native double to Wrapper long without doing the intermediate casting. 有没有任何方法可以将本机double长时间转换为Wrapper,而无需进行中间转换。

I am concerned because there are numerous locations where i need to do this. 我很担心,因为在很多地方我都需要这样做。 Thanks. 谢谢。

Instead of doing the casting in every java statement, you can create static method which takes a double value as argument, do the casting and return a Long object. 您可以创建一个以双精度值作为参数的静态方法,而不是在每个java语句中进行强制转换,然后进行强制转换并返回Long对象。 In this way, it's easy to maintain and the reusabilty of the code without redundancy 这样,易于维护,代码的可重复使用性而无需冗余

Just cast and java autoboxing will convert to the Long object type for you. 只是强制转换和Java自动装箱会为您转换为Long对象类型。 No need to make special libraries or extra methods (stay lean!) 无需制作特殊的库或其他方法(保持精简!)

linkcatch.setCode((long)relatedCd.getDouble(value));

Notes: 笔记:

  • You might want to round instead of just casting. 您可能需要舍入而不是仅投射。 ( double to long conversion ) 双倍到长转换
  • I see you have a 'realtedCd' object which means you might be dealing with money or arithmetic operations. 我看到您有一个“ realtedCd”对象,这意味着您可能正在处理货币或算术运算。 If so, I'd advise using BigDecimal instead of doubles for precision Double vs. BigDecimal? 如果是这样,我建议使用BigDecimal而不是double来实现Double和BigDecimal的精度

Found a shorter way to make it. 找到了一种更短的方法。 You can use Math.Round to get the long native value. 您可以使用Math.Round来获取长本机值。 After that you can simply cast it to long 之后,您只需将其转换为长

double a = -5.42
Long = (Long)Math.round(a);

However another approach would be too do something like making another class to do the work for you. 但是,另一种方法也可以做一些事情,例如开设另一个班级来为您完成工作。

public class Util{

    public static Long getLong(double a){
         return (Long)Math.round(a);
    }
}

This way your code would look something like this 这样,您的代码将如下所示

linkcatch.setCode(Util.getLong(value));

instead of 代替

linkcatch.setCode(Long.valueOf((long) relatedCd.getDouble(value)));

or 要么

linkcatch.setCode(Long.valueOf(String.valueOf(relatedCd.getDouble(value))));

Hope it helps your to make your code cleaner and easier to manage. 希望它可以帮助您使代码更整洁,更易于管理。 Also if you want all the Long values to Round up or down, you have a single place to edit(The Util class), instead of 100 different. 另外,如果希望所有“长整数”值都向上或向下取整,则可以在单个位置(Util类)进行编辑,而不是100个不同的值。

You can do like this: 您可以这样:

Long l = new Double(relatedCd.getDouble(value)).longValue();

Java autoboxing will help you do other things. Java自动装箱将帮助您执行其他操作。

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

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