无法将字符串解析为长整型并在列表中使用。

huangapple go评论60阅读模式
英文:

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

问题

我从服务器获取了一些长整型值 getJan();,并且必须对这个值进行格式化,以便添加一些逗号,使其看起来更美观。然后将这些值放入接受长整型的列表中,但是 DecimalFormat 返回的是字符串。我必须将其转换为长整型,以便将它们放入我的列表中。我尝试过 Long.valueOf()Long.parseLong(),但是值没有显示出来。

对于这个帖子,我只放了一个值,但将来会有更多的值,所以我正在通过列表进行循环。

我的代码:

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()));

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

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

I am getting some values in 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. 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.

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&lt;Entry&gt; lista = new ArrayList&lt;&gt;();

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


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

        List&lt;Long&gt; listOfMonth = new ArrayList&lt;&gt;();
        listOfMonth.add(jan);
        

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

答案1

得分: 1

一个长整型(long)或整型(int)不能具有任何小数值。对于小数值,您必须使用浮点型(float)或双精度浮点型(double)。但是,如果您想要格式化该值并在列表中显示,您可能应该使用字符串(string)。

英文:

A long or an int can not have any decimal values. For decimal values you have to use either float or double. But if you want to format the value and display in a list you should probably go with a string.

huangapple
  • 本文由 发表于 2020年10月7日 22:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64246129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定