2

I am using EazeGraph library to plot my Double values

  // in percent
    Double PROTEIN_percent = ((PROTEIN_grams / TOTALCALORIES_numbers) * 100);
    Double FAT_percent = ((FAT_grams / TOTALCALORIES_numbers) * 100);
    Double CARBS_percent = ((CARBS_grams / TOTALCALORIES_numbers) * 100);

The problem is it does not let me plot it without adding .floatValue to the Double

  mPieChart.addPieSlice(new PieModel("CARBS", CARBS_percent.floatValue(), Color.parseColor("#FE6DA8")));
    mPieChart.addPieSlice(new PieModel("PROTEIN", PROTEIN_percent.floatValue(), Color.parseColor("#56B7F1")));
    mPieChart.addPieSlice(new PieModel("FAT", FAT_percent.floatValue(), Color.parseColor("#FED70E")));
    mPieChart.startAnimation();

Also I want the output to be rounded to 2 digits, It does not let me use String.format("%.2f" because it is not a string.

This is what I am getting : 62.1232131342

and this is what I want: 62.12

  • I guess this is not a duplicate because the problem relies on the library itself, rather, this is not a proper question here. This question must be raised on the library itself, EazeGraph. If they can add or they have an option to format the value. – Tenten Ponce Dec 26 '17 at 06:21

4 Answers4

1

It should work.

String twoDecimalResult = String.format("%.2f", CARBS_percent);
Suresh Atta
  • 114,879
  • 36
  • 179
  • 284
0

You have two options:

1st Option: Disable decimal

This option is not actually a solution, because from what I saw on their library, it you can only set a custom inner value on the piechart, which leads us to the second option:

mPieChart.setShowDecimal(false);

2nd Option: Put a listener to manually change the inner value of the pie chart.

public static String formatTwoDecimal(double d) {
   NumberFormat numberFormat = new DecimalFormat("#,##0.00");

   return numberFormat.format(d);
}

=======

mPieChart.setUseCustomInnerValue(true); //to override the inner value
mPieChart.setOnItemFocusChangedListener(new IOnItemFocusChangedListener() {
   @Override
   void onItemFocusChanged(int _Position) {
      //_Position is the position of the pie you inserted, in your case, 0 will be the CARBS, 1 is PROTEIN, 2 is FAT
      if (_Position == 0) {
         mPieChart.setInnerValueString(formatTwoDecimal(CARBS_percent));
      } else if (_Position == 1) {
         mPieChart.setInnerValueString(formatTwoDecimal(PROTEIN_percent));
      } else if (_Position == 2) {
         mPieChart.setInnerValueString(formatTwoDecimal(FAT_percent));
      }
   }
});

I haven't actually tried this but base on my review on their library, this will work. Tell me if you find any errors on this method.

Tenten Ponce
  • 2,256
  • 1
  • 9
  • 27
  • Sorry, it seems that your problem occur on the library itself, the EazeGraph. They are not rounding it off to two decimal. I guess you can raise the question as an issue on their github. https://github.com/blackfizz/EazeGraph – Tenten Ponce Dec 26 '17 at 06:18
  • I used `("CARBS", formatTwoDecimal(CARBS_percent).length(), Color` and it worked! but is it okay to use that? –  Dec 26 '17 at 06:25
  • What are the outputs? I guess the output will just be the length of the string, not the actual number, you only just get the length and pass it on the parameter. I've revied the PieModel and it just accepts float as 2nd parameter. – Tenten Ponce Dec 26 '17 at 06:30
  • yea, the outputs are messed up. they are 4, 4 , 7. I was expecting it in hundreds. –  Dec 26 '17 at 06:38
  • Well I guess so. Ill try to review the library if somehow we can hack or there's an option. – Tenten Ponce Dec 26 '17 at 06:40
  • Thanks so much ! –  Dec 26 '17 at 06:41
0

You can do like that

Float.parseFloat(String.format("%.2f", 62.1232131342));
Hardik Mehta
  • 125
  • 8
0

Try this

DecimalFormat decimalFormat = new DecimalFormat("#0.00");

decimalFormat.format(Double.parseDouble(" your value "))
Ratilal Chopda
  • 4,014
  • 4
  • 15
  • 28