Home | History | Annotate | Download | only in number-format
      1 // Copyright 2015 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Make sure minimumFractionDigits and maximumFractionDigits are honored
      6 
      7 var nf = new Intl.NumberFormat("en-us", { useGrouping: false, minimumFractionDigits: 4, maximumFractionDigits: 8});
      8 
      9 assertEquals("12345.6789", nf.format(12345.6789));
     10 assertEquals("12345.678912", nf.format(12345.678912));
     11 assertEquals("12345.6700", nf.format(12345.67));
     12 assertEquals("12345.67891234", nf.format(12345.6789123421));
     13 
     14 nf = new Intl.NumberFormat("en-us", { useGrouping: false, minimumFractionDigits: 4, maximumFractionDigits: 8, style: 'percent'});
     15 
     16 assertEquals("12345.6789%", nf.format(123.456789));
     17 assertEquals("12345.678912%", nf.format(123.45678912));
     18 assertEquals("12345.6700%", nf.format(123.4567));
     19 assertEquals("12345.67891234%", nf.format(123.456789123421));
     20 
     21 nf = new Intl.NumberFormat('en', {minimumFractionDigits: 4, maximumFractionDigits: 8, style: 'currency', currency: 'USD'});
     22 
     23 assertEquals("$54,306.404797", nf.format(54306.4047970));
     24 assertEquals("$54,306.4000", nf.format(54306.4));
     25 assertEquals("$54,306.40000001", nf.format(54306.400000011));
     26 
     27 // Ensure that appropriate defaults exist when minimum and maximum are not specified
     28 
     29 nf = new Intl.NumberFormat("en-us", { useGrouping: false });
     30 
     31 assertEquals("12345.679", nf.format(12345.6789));
     32 assertEquals("12345.679", nf.format(12345.678912));
     33 assertEquals("12345.67", nf.format(12345.6700));
     34 assertEquals("12345", nf.format(12345));
     35 assertEquals("12345.679", nf.format(12345.6789123421));
     36 
     37 nf = new Intl.NumberFormat("en-us", { useGrouping: false, style: 'percent'});
     38 
     39 assertEquals("12346%", nf.format(123.456789));
     40 assertEquals("12346%", nf.format(123.45678912));
     41 assertEquals("12346%", nf.format(123.456700));
     42 assertEquals("12346%", nf.format(123.456789123421));
     43 assertEquals("12345%", nf.format(123.45));
     44 
     45 // For currency, the minimum or the maximum can be overwritten individually
     46 
     47 nf = new Intl.NumberFormat('en', {minimumFractionDigits: 0, style: 'currency', currency: 'USD'});
     48 
     49 assertEquals("$54,306.4", nf.format(54306.4047970));
     50 assertEquals("$54,306.4", nf.format(54306.4));
     51 assertEquals("$54,306", nf.format(54306));
     52 
     53 nf = new Intl.NumberFormat('en', {maximumFractionDigits: 3, style: 'currency', currency: 'USD'});
     54 
     55 assertEquals("$54,306.405", nf.format(54306.4047970));
     56 assertEquals("$54,306.40", nf.format(54306.4));
     57 assertEquals("$54,306.00", nf.format(54306));
     58