简体   繁体   English

无法将 String 解析为 Long 并在列表中使用它

[英]Can't parse String to Long and use it in list

I am getting some values in long getJan();我在 long getJan(); from server and has to format this value so that i have some commas and look nicer.来自服务器并且必须格式化此值,以便我有一些逗号并且看起来更好。 And put that values in List that accept longs, but DecimalFormat is returning String.并将这些值放入接受 long 的 List 中,但 DecimalFormat 返回 String。 I must covert to long so that I can put them in my list.我必须隐蔽很久才能把他们列入我的名单。 I tried both Long.valueOf() and Long.parseLong() but values are not shown.我尝试了Long.valueOf()Long.parseLong()但未显示值。

For this post i put only one value but in future it will be more that why I am looping through list.对于这篇文章,我只给出了一个值,但将来它会更多地说明我循环列表的原因。

my code:我的代码:

List<Entry> lista = new ArrayList<>();

        NumberFormat myFormat = NumberFormat.getNumberInstance(Locale.US);
        myFormat.setGroupingUsed(true);


        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        decimalFormat.setGroupingUsed(true);
        decimalFormat.setGroupingSize(3);
        Long jan = Long.parseLong(decimalFormat.format(netWorthPerMonth.getJan()));
     //   Log.i(TAG, "setSingleLineChart: " + jan);

        List<Long> listOfMonth = new ArrayList<>();
        listOfMonth.add(jan);
        

        for (int i = 0; i<  handleXAxis().size();i++){
            lista.add(new Entry(i, listOfMonth.get(i) ));
        }

A long or an int can not have any decimal values. longint不能有任何十进制值。 For decimal values you have to use either float ordouble .对于十进制值,您必须使用floatdouble But if you want to format the value and display in a list you should probably go with astring .但是,如果您想格式化值并显示在列表中,您可能应该使用 go字符串

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

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