コード例 #1
0
 public static String formatFloatNumber(Double value) {
   if (value != null) {
     if (value.doubleValue() != 0.00) {
       java.text.DecimalFormat df = new java.text.DecimalFormat("###.00");
       return df.format(value.doubleValue());
     } else {
       return "0.00";
     }
   }
   return "";
 }
    /**
     * Gets the displayed value, and uses the converter to convert the shared value to units this
     * instance is supposed to represent. If the displayed value and shared value are different,
     * then we turn off our document filter and update our value to match the shared value. We
     * re-enable the document filter afterwards.
     */
    @Override
    protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      try {
        StyledDocument doc = getStyledDocument();
        double actualValue = (multiplier.getDisplayValue(value.getValue()));
        String actualValueAsString = df.format(actualValue);

        String displayedValue;
        try {
          displayedValue = doc.getText(0, doc.getLength());
        } catch (BadLocationException e) {
          displayedValue = "";
        }
        if (displayedValue.isEmpty()) {
          displayedValue = "0";
        }
        double displayedValueAsDouble = Double.parseDouble(displayedValue);
        if (!actualValueAsString.equals(displayedValue)
            && displayedValueAsDouble != actualValue) // Allow user to enter trailing zeroes.
        {
          bypassFilterAndSetText(doc, actualValueAsString);
        }
      } catch (NumberFormatException e) {
        // Swallow it as it's OK for the user to try and enter non-numbers.
      }
    }
コード例 #3
0
ファイル: Emotion.java プロジェクト: petercrlane/chrest
  public String displayNONUTF() {
    List<BasicEmotion> sortedemotions = new ArrayList<BasicEmotion>(_components.size());
    String resultstring = "";

    // build ArrayList sorted by percentages

    for (BasicEmotion basicemotion : _components.keySet()) {
      if (sortedemotions.size() == 0 && _components.get(basicemotion) != 0) { // was >0
        sortedemotions.add(basicemotion);
      } else {
        boolean done = false;
        int index = sortedemotions.size();
        for (int i = 0; i < sortedemotions.size(); i++) {
          BasicEmotion sortedbasicemotion = sortedemotions.get(i);
          double current_perc = _components.get(basicemotion);
          double running_perc = _components.get(sortedbasicemotion);
          if (current_perc <= running_perc && done == false) {
            index = i;
            done = true;
          }
        }

        if (_components.get(basicemotion) != 0) // >0
        {
          sortedemotions.add(index, basicemotion);
        }
      }
    }

    for (BasicEmotion basicemotion : sortedemotions) {
      String symbol = "";
      if (basicemotion.getTheory() == EmotionTheory.PLUTCHIK) {
        PlutchikBasicEmotion pbe = (PlutchikBasicEmotion) basicemotion;
        switch (pbe) {
          case JOY:
            symbol = "joy";
            break;
          case FEAR:
            symbol = "fear";
            break;
          case SADNESS:
            symbol = "sad";
            break;
          case SURPRISE:
            symbol = "!";
            break;
          case ANTICIPATION:
            symbol = "anticip";
            break;
          case TRUST:
            symbol = "trust";
            break;
          case ANGER:
            symbol = "anger";
            break;
          case DISGUST:
            symbol = "disgust";
            break;
        }
      }
      if (basicemotion.getTheory() == EmotionTheory.EKMAN) {
        EkmanBasicEmotion pbe = (EkmanBasicEmotion) basicemotion;
        switch (pbe) {
          case AMUSEMENT:
            symbol = "amus.";
            break;
          case ANGER:
            symbol = "ang.";
            break;
          case CONTEMPT:
            symbol = "contempt";
            break;
          case CONTENTMENT:
            symbol = "contentm.";
            break;
          case DISGUST:
            symbol = "disg.";
            break;
          case EMBARASSMENT:
            symbol = "emb.";
            break;
          case EXCITEMENT:
            symbol = "exc.";
            break;
          case FEAR:
            symbol = "fear";
            break;
          case GUILT:
            symbol = "guilt";
            break;
          case PRIDEINACHIEVEMENT:
            symbol = "pr.ach.";
            break;
          case RELIEF:
            symbol = "rel.";
            break;
          case SADNESSDISTRESS:
            symbol = "sadn.dis.";
            break;
          case SATISFACTION:
            symbol = "sat.";
            break;
          case SENSORYPLEASURE:
            symbol = "sens.pleas.";
            break;
          case SHAME:
            symbol = "shame";
            break;
        }
      }
      // resultstring = symbol + String.valueOf(_components.get(basicemotion)) + " " + resultstring;
      java.text.DecimalFormat df = new java.text.DecimalFormat(".000");
      if (_components.get(basicemotion) == 0) {
      } else {
        resultstring =
            symbol
                + " "
                + String.valueOf(df.format(_components.get(basicemotion)))
                + " "
                + resultstring;
      }
    }

    return resultstring;
  }