[ 
https://issues.apache.org/jira/browse/LANG-1109?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14502215#comment-14502215
 ] 

Marco Janc commented on LANG-1109:
----------------------------------

Oh your comments/mails were moved to spam folder so did not saw them.

Using java percentage formatter formats number '0.123' as '12%' instead of 
'12,3%'.

Here some inputs and outputs of my function with a JUnit test.

{code}
@Test
public void testFormatPercentScale()
{
    final String exp1 = "0,1%";
    final String exp2 = "1%";
    final String exp3 = "1,5%";

    Locale locale = Locale.German;

    Assert.assertEquals(exp1, formatPercentFraction(0.001, locale));
    Assert.assertEquals(exp2, formatPercentFraction(0.01, locale));
    Assert.assertEquals(exp3, formatPercentFraction(0.015, locale));

    Assert.assertEquals(exp1, formatPercentFraction(0.001f, locale));
    Assert.assertEquals(exp2, formatPercentFraction(0.01f, locale));
    Assert.assertEquals(exp3, formatPercentFraction(0.015f, locale));

    Assert.assertEquals(exp1, formatPercentFraction(0.001d, locale));
    Assert.assertEquals(exp2, formatPercentFraction(0.01d, locale));
    Assert.assertEquals(exp3, formatPercentFraction(0.015d, locale));

    Assert.assertEquals(exp1, formatPercentFraction(new BigDecimal("0.001"), 
locale));
    Assert.assertEquals(exp2, formatPercentFraction(new BigDecimal("0.01"), 
locale));
    Assert.assertEquals(exp3, formatPercentFraction(new BigDecimal("0.015"), 
locale));

    Assert.assertEquals(exp1, formatPercentFraction(new BigDecimal(0.001), 
locale));
    Assert.assertEquals(exp2, formatPercentFraction(new BigDecimal(0.01), 
locale));
    Assert.assertEquals(exp3, formatPercentFraction(new BigDecimal(0.015), 
locale));

    Assert.assertEquals(exp1, formatPercentFraction(new BigDecimal(0.001f), 
locale));
    Assert.assertEquals(exp2, formatPercentFraction(new BigDecimal(0.01f), 
locale));
    Assert.assertEquals(exp3, formatPercentFraction(new BigDecimal(0.015f), 
locale));

    Assert.assertEquals(exp1, formatPercentFraction(new BigDecimal(0.001d), 
locale));
    Assert.assertEquals(exp2, formatPercentFraction(new BigDecimal(0.01d), 
locale));
    Assert.assertEquals(exp3, formatPercentFraction(new BigDecimal(0.015d), 
locale));

    // test with different scale
    final BigDecimal value1 = new BigDecimal(0.001f);
    value1.setScale(4, RoundingMode.HALF_UP);
    final BigDecimal value2 = new BigDecimal(0.01f);
    value2.setScale(4, RoundingMode.HALF_UP);
    final BigDecimal value3 = new BigDecimal(0.015f);
    value3.setScale(4, RoundingMode.HALF_UP);
    Assert.assertEquals(exp1, formatPercentFraction(value1, locale));
    Assert.assertEquals(exp2, formatPercentFraction(value2, locale));
    Assert.assertEquals(exp3, formatPercentFraction(value3, locale));
}
{code}

> Number percentage formatting with fractional digits
> ---------------------------------------------------
>
>                 Key: LANG-1109
>                 URL: https://issues.apache.org/jira/browse/LANG-1109
>             Project: Commons Lang
>          Issue Type: New Feature
>          Components: lang.*
>            Reporter: Marco Janc
>             Fix For: Discussion
>
>
> Java built-in number formatter does formats Number locale aware with 
> fractional digits defined by the defined scale of the Number, aswell the 
> required precision (trims trailing zeros).
> For some reason Java's built-in percentage number formatter does not formats 
> fractional digits. So i wrote a function which has same behavior as the Java 
> built-in number formatter but with percentage formatting.
> {code:java}
>       /**
>        * Formats the given Number as percentage with necessary precision.
>        * This serves as a workaround for {@link 
> NumberFormat#getPercentInstance()} which does not renders fractional
>        * digits.
>        *
>        * @param number
>        * @param locale
>        *
>        * @return
>        */
>       public static String formatPercentFraction(final Number number, final 
> Locale locale)
>       {
>               if (number == null)
>                       return null;
>               // get string representation with dot
>               final String strNumber = 
> NumberFormat.getNumberInstance(Locale.US).format(number.doubleValue());
>               // create exact BigDecimal and convert to get scale
>               final BigDecimal dNumber = new 
> BigDecimal(strNumber).multiply(new BigDecimal(100));
>               final NumberFormat percentScaleFormat = 
> NumberFormat.getPercentInstance(locale);
>               percentScaleFormat.setMaximumFractionDigits(Math.max(0, 
> dNumber.scale()));
>               // convert back for locale percent formatter
>               return percentScaleFormat.format(dNumber.multiply(new 
> BigDecimal(0.01)));
>       }
> {code}
> I also unit tested it with many inputs.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to