简体   繁体   中英

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

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<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. For decimal values you have to use either float ordouble . But if you want to format the value and display in a list you should probably go with astring .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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