Example usage for java.lang Number toString

List of usage examples for java.lang Number toString

Introduction

In this page you can find the example usage for java.lang Number toString.

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:uk.ac.diamond.scisoft.analysis.rcp.views.PlotViewStatsAndMaths.java

private String formatNumber(Number n) {
    if (n instanceof Double || n instanceof Float) {
        return String.format(doubleFormat, n.doubleValue());
    }//from   w ww  . j  a  v a  2s.  c o m
    return n.toString();
}

From source file:org.sakaiproject.evaluation.tool.reporting.LikertPercentageItemLabelGenerator.java

public String generateLabel(CategoryDataset dataset, int series, int category) {
    Number value = dataset.getValue(series, category);
    if (value == null)
        return "";
    double doubleVal = value.doubleValue();
    int intVal = value.intValue();
    if (doubleVal == 0.0) {
        return "0 % (0)";
    } else if (totalItems != 0) {

        double percentage = doubleVal / doubleTotalItems * 100;
        // return percentage + " % (" + intVal + ")";
        // return String.format("%.2f %s (%s)", percentage,"%", intVal+"");
        return String.format("%.0f %s (%s)", percentage, "%", intVal + "");
    } else if (value != null) {
        return value.toString();
    }/* w w  w.  ja va  2 s  .  c o  m*/
    return "";
}

From source file:org.pentaho.platform.uifoundation.chart.XYSeriesCollectionChartComponent.java

@Override
public Document getXmlContent() {

    // Create a document that describes the result
    Document result = DocumentHelper.createDocument();
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    String contextPath = requestContext.getContextPath();

    setXslProperty("baseUrl", contextPath + "/"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String mapName = "chart" + AbstractChartComponent.chartCount++; //$NON-NLS-1$
    Document chartDefinition = jcrHelper.getSolutionDocument(definitionPath, RepositoryFilePermission.READ);

    if (chartDefinition == null) {
        Element errorElement = result.addElement("error"); //$NON-NLS-1$
        errorElement.addElement("title").setText( //$NON-NLS-1$
                Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$
        String message = Messages.getInstance().getString("CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", //$NON-NLS-1$
                definitionPath);/*from   ww  w. ja  v  a  2 s  . c  om*/
        errorElement.addElement("message").setText(message); //$NON-NLS-1$
        error(message);
        return result;
    }
    // create a pie definition from the XML definition
    dataDefinition = createChart(chartDefinition);

    if (dataDefinition == null) {
        Element errorElement = result.addElement("error"); //$NON-NLS-1$
        errorElement.addElement("title").setText( //$NON-NLS-1$
                Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$
        String message = Messages.getInstance().getString("CHARTS.ERROR_0002_CHART_DATA_MISSING", actionPath); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        errorElement.addElement("message").setText(message); //$NON-NLS-1$
        // System .out.println( result.asXML() );
        return result;
    }

    // create an image for the dial using the JFreeChart engine
    PrintWriter printWriter = new PrintWriter(new StringWriter());
    // we'll dispay the title in HTML so that the dial image does not have
    // to
    // accommodate it
    String chartTitle = ""; //$NON-NLS-1$
    try {
        if (width == -1) {
            width = Integer.parseInt(chartDefinition.selectSingleNode("/chart/width").getText()); //$NON-NLS-1$
        }
        if (height == -1) {
            height = Integer.parseInt(chartDefinition.selectSingleNode("/chart/height").getText()); //$NON-NLS-1$
        }
    } catch (Exception e) {
        // go with the default
    }
    if (chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME) != null) { //$NON-NLS-1$
        urlTemplate = chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME) //$NON-NLS-1$
                .getText();
    }

    if (chartDefinition.selectSingleNode("/chart/paramName") != null) { //$NON-NLS-1$
        paramName = chartDefinition.selectSingleNode("/chart/paramName").getText(); //$NON-NLS-1$
    }

    Element root = result.addElement("charts"); //$NON-NLS-1$
    XYSeriesCollection chartDataDefinition = (XYSeriesCollection) dataDefinition;
    if (chartDataDefinition.getSeriesCount() > 0) {
        // create temporary file names
        String[] tempFileInfo = createTempFile();
        String fileName = tempFileInfo[AbstractChartComponent.FILENAME_INDEX];
        String filePathWithoutExtension = tempFileInfo[AbstractChartComponent.FILENAME_WITHOUT_EXTENSION_INDEX];

        ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
        JFreeChartEngine.saveChart(chartDataDefinition, chartTitle, "", filePathWithoutExtension, width, height, //$NON-NLS-1$
                JFreeChartEngine.OUTPUT_PNG, printWriter, info, this);
        applyOuterURLTemplateParam();
        populateInfo(info);
        Element chartElement = root.addElement("chart"); //$NON-NLS-1$
        chartElement.addElement("mapName").setText(mapName); //$NON-NLS-1$
        chartElement.addElement("width").setText(Integer.toString(width)); //$NON-NLS-1$
        chartElement.addElement("height").setText(Integer.toString(height)); //$NON-NLS-1$
        for (int row = 0; row < chartDataDefinition.getSeriesCount(); row++) {
            for (int column = 0; column < chartDataDefinition.getItemCount(row); column++) {
                Number value = chartDataDefinition.getY(row, column);
                Comparable rowKey = chartDataDefinition.getSeriesKey(row);
                Number columnKey = chartDataDefinition.getX(row, column);
                Element valueElement = chartElement.addElement("value2D"); //$NON-NLS-1$
                valueElement.addElement("value").setText(value.toString()); //$NON-NLS-1$
                valueElement.addElement("row-key").setText(rowKey.toString()); //$NON-NLS-1$
                valueElement.addElement("column-key").setText(columnKey.toString()); //$NON-NLS-1$
            }
        }
        String mapString = ImageMapUtilities.getImageMap(mapName, info);
        chartElement.addElement("imageMap").setText(mapString); //$NON-NLS-1$
        chartElement.addElement("image").setText(fileName); //$NON-NLS-1$
    }
    return result;
}

From source file:org.pentaho.platform.uifoundation.chart.XYZSeriesCollectionChartComponent.java

@Override
public Document getXmlContent() {

    // Create a document that describes the result
    Document result = DocumentHelper.createDocument();
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    setXslProperty("baseUrl", requestContext.getContextPath()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    setXslProperty("fullyQualifiedServerUrl", //$NON-NLS-1$
            PentahoSystem.getApplicationContext().getFullyQualifiedServerURL()); //$NON-NLS-2$ //$NON-NLS-3$
    String mapName = "chart" + AbstractChartComponent.chartCount++; //$NON-NLS-1$
    Document chartDefinition = jcrHelper.getSolutionDocument(definitionPath, RepositoryFilePermission.READ);

    if (chartDefinition == null) {
        Element errorElement = result.addElement("error"); //$NON-NLS-1$
        errorElement.addElement("title").setText( //$NON-NLS-1$
                Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$
        String message = Messages.getInstance().getString("CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", //$NON-NLS-1$
                definitionPath);//from  w  w  w  . j  ava2  s  . com
        errorElement.addElement("message").setText(message); //$NON-NLS-1$
        error(message);
        return result;
    }
    // create a pie definition from the XML definition
    dataDefinition = createChart(chartDefinition);

    if (dataDefinition == null) {
        Element errorElement = result.addElement("error"); //$NON-NLS-1$
        errorElement.addElement("title").setText( //$NON-NLS-1$
                Messages.getInstance().getString("ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART")); //$NON-NLS-1$
        String message = Messages.getInstance().getString("CHARTS.ERROR_0002_CHART_DATA_MISSING", actionPath); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        errorElement.addElement("message").setText(message); //$NON-NLS-1$
        // System .out.println( result.asXML() );
        return result;
    }

    // create an image for the dial using the JFreeChart engine
    PrintWriter printWriter = new PrintWriter(new StringWriter());
    // we'll dispay the title in HTML so that the dial image does not have
    // to
    // accommodate it
    String chartTitle = ""; //$NON-NLS-1$
    try {
        if (width == -1) {
            width = Integer.parseInt(chartDefinition.selectSingleNode("/chart/width").getText()); //$NON-NLS-1$
        }
        if (height == -1) {
            height = Integer.parseInt(chartDefinition.selectSingleNode("/chart/height").getText()); //$NON-NLS-1$
        }
    } catch (Exception e) {
        // go with the default
    }
    if (chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME) != null) { //$NON-NLS-1$
        urlTemplate = chartDefinition.selectSingleNode("/chart/" + AbstractChartComponent.URLTEMPLATE_NODE_NAME) //$NON-NLS-1$
                .getText();
    }

    if (chartDefinition.selectSingleNode("/chart/paramName") != null) { //$NON-NLS-1$
        paramName = chartDefinition.selectSingleNode("/chart/paramName").getText(); //$NON-NLS-1$
    }

    Element root = result.addElement("charts"); //$NON-NLS-1$
    XYZSeriesCollectionChartDefinition chartDataDefinition = (XYZSeriesCollectionChartDefinition) dataDefinition;
    if (chartDataDefinition.getSeriesCount() > 0) {
        // create temporary file names
        String[] tempFileInfo = createTempFile();
        String fileName = tempFileInfo[AbstractChartComponent.FILENAME_INDEX];
        String filePathWithoutExtension = tempFileInfo[AbstractChartComponent.FILENAME_WITHOUT_EXTENSION_INDEX];

        ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
        JFreeChartEngine.saveChart(chartDataDefinition, chartTitle, "", filePathWithoutExtension, width, height, //$NON-NLS-1$
                JFreeChartEngine.OUTPUT_PNG, printWriter, info, this);
        applyOuterURLTemplateParam();
        populateInfo(info);
        Element chartElement = root.addElement("chart"); //$NON-NLS-1$
        chartElement.addElement("mapName").setText(mapName); //$NON-NLS-1$
        chartElement.addElement("width").setText(Integer.toString(width)); //$NON-NLS-1$
        chartElement.addElement("height").setText(Integer.toString(height)); //$NON-NLS-1$
        for (int row = 0; row < chartDataDefinition.getSeriesCount(); row++) {
            for (int column = 0; column < chartDataDefinition.getItemCount(row); column++) {
                Number value = chartDataDefinition.getY(row, column);
                Comparable rowKey = chartDataDefinition.getSeriesKey(row);
                Number columnKey = chartDataDefinition.getX(row, column);
                Element valueElement = chartElement.addElement("value2D"); //$NON-NLS-1$
                valueElement.addElement("value").setText(value.toString()); //$NON-NLS-1$
                valueElement.addElement("row-key").setText(rowKey.toString()); //$NON-NLS-1$
                valueElement.addElement("column-key").setText(columnKey.toString()); //$NON-NLS-1$
            }
        }
        String mapString = ImageMapUtilities.getImageMap(mapName, info);
        chartElement.addElement("imageMap").setText(mapString); //$NON-NLS-1$
        chartElement.addElement("image").setText(fileName); //$NON-NLS-1$
    }
    return result;
}

From source file:com.prowidesoftware.swift.model.field.Field.java

/**
 * Gets a BigDecimal from a generic Number argument
 * @param number//from  w w  w.j a va  2 s. c  o m
 * @return BigDecimal value of number parameter
 */
static public BigDecimal getAsBigDecimal(final Number number) {
    if (number instanceof BigDecimal) {
        return (BigDecimal) number;
    } else if (number instanceof Long) {
        return new BigDecimal(((Long) number).longValue());
    } else if (number instanceof Integer) {
        return new BigDecimal(((Integer) number).intValue());
    } else if (number instanceof Short) {
        return new BigDecimal(((Short) number).intValue());
    } else if (number instanceof Double) {
        return new BigDecimal(number.toString());
    } else {
        throw new IllegalArgumentException("class " + number.getClass().getName() + " is not supported");
    }
}

From source file:org.evosuite.testcase.statements.FunctionalMockStatementTest.java

@Test
public void testConfirmNumber() {
    String foo = "foo";
    Number number = mock(Number.class);
    when(number.toString()).thenReturn(foo);

    assertEquals(foo, number.toString());
}

From source file:com.dream.messaging.engine.DataHandler.java

/**
 * Format number data.//from w ww.  j  a v  a 2 s.co m
 * 
 * @param value the value
 * @param node the node
 * 
 * @return the string
 */
protected String formatNumberData(Number value, Node node) {
    String format = QueryNode.getAttribute(node, ElementAttr.Attr_Format);
    if (format == null) {
        return value.toString();
    }
    try {

        ElementFormat eformat = new ElementFormat(format);
        int max = eformat.getMaxLen();
        int min = eformat.getMinLen();
        if (max < min)
            max = min;
        String pattern = (max > min) ? Strings.fill('#', max - 1) : Strings.fill('#', min - 1);
        pattern += "0";
        double num = value.doubleValue();
        double MAX = Double
                .parseDouble(Strings.fill('9', max) + "." + Strings.fill('9', eformat.getFraction()));
        if (num > MAX) {
            value = new Double(MAX);
        }
        double MIN = 0;
        if (eformat.isHasPoint()) {
            pattern += "." + Strings.fill('0', eformat.getFraction());
            MIN = -MAX;
        }
        if (num < MIN) {

            value = new Double(MIN);
        }
        String append = format(value, pattern);
        return append;
    } catch (ElementFormatParseException e) {

        return null;
    }

}

From source file:org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQueryCriteria.java

private List<String> getNumberListAsStringList(List<Number> numberList) {
    List<String> list = new ArrayList<String>();
    for (Number number : numberList) {
        if (number != null) {
            list.add(number.toString());
        } else {/*from   w ww .j av  a 2  s.c om*/
            list.add(null);
        }
    }
    return list;
}

From source file:com.xie.javacase.json.JSONObject.java

/**
 * Produce a string from a Number./*from  w  ww.j  a  v a2  s. co  m*/
 * @param  n A Number
 * @return A String.
 * @throws org.json.JSONException If n is a non-finite number.
 */
static public String numberToString(Number n) throws JSONException {
    if (n == null) {
        throw new JSONException("Null pointer");
    }
    testValidity(n);

    // Shave off trailing zeros and decimal point, if possible.

    String s = n.toString();
    if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) {
        while (s.endsWith("0")) {
            s = s.substring(0, s.length() - 1);
        }
        if (s.endsWith(".")) {
            s = s.substring(0, s.length() - 1);
        }
    }
    return s;
}

From source file:org.orcid.core.cli.MigrateFundingAmountToANumericValue.java

private BigDecimal getAmountAsBigDecimal(String amount, String currencyCode, Locale locale) throws Exception {
    try {/*from  ww w  .j a v  a 2 s. co m*/
        ParsePosition parsePosition = new ParsePosition(0);
        NumberFormat numberFormat = NumberFormat.getInstance(locale);
        Number number = null;
        if (!PojoUtil.isEmpty(currencyCode)) {
            Currency currency = Currency.getInstance(currencyCode);
            String currencySymbol = currency.getSymbol();
            number = numberFormat.parse(amount.replace(currencySymbol, StringUtils.EMPTY), parsePosition);
        } else {
            number = numberFormat.parse(amount, parsePosition);
        }
        if (parsePosition.getIndex() != amount.length())
            throw new Exception("Unable to parse amount into BigDecimal");
        return new BigDecimal(number.toString());
    } catch (Exception e) {
        throw e;
    }
}