How we can format the double value to two decimal places

  1. #1

    How we can format the double value to two decimal places

    Hi,

    How we can format the double value to two decimal places.I used this function.

    <script type="text/javascript">
    
        Ext.apply(Ext.util.Format, {
            number: function(v, format) {
                if (!format) {
                    return v;
                }
    
                v *= 1;
                if (typeof v != 'number' || isNaN(v)) {
                    return '';
                }
                var comma = ',';
                var dec = '.';
                var i18n = false;
    
                if (format.substr(format.length - 2) == '/i') {
                    format = format.substr(0, format.length - 2);
                    i18n = true;
                    comma = '.';
                    dec = ',';
                }
    
                var hasComma = format.indexOf(comma) != -1,
                        psplit = (i18n ? format.replace(/[^\d\,]/g, '') : format.replace(/[^\d\.]/g, '')).split(dec);
    
                if (1 < psplit.length) {
                    v = v.toFixed(psplit[1].length);
                }
                else if (2 < psplit.length) {
                    throw ('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
                }
                else {
                    v = v.toFixed(0);
                }
    
                var fnum = v.toString();
    
                if (hasComma) {
                    psplit = fnum.split('.');
    
                    var cnum = psplit[0],
                            parr = [],
                            j = cnum.length,
                            m = Math.floor(j / 3),
                            n = cnum.length % 3 || 3;
    
                    for (var i = 0; i < j; i += n) {
                        if (i != 0) { n = 3; }
                        parr[parr.length] = cnum.substr(i, n);
                        m -= 1;
                    }
                    fnum = parr.join(comma);
                    if (psplit[1]) {
                        fnum += dec + psplit[1];
                    }
                }
    
                return format.replace(/[\d,?\.?]+/, fnum);
            },
    
            numberRenderer: function(format) {
                return function(v) {
                    return Ext.util.Format.number(v, format);
                };
            }
        });
        </script>
    <ext:GridPanel 
                ID="GridPanel1" 
                runat="server" 
                StoreID="stockstore" 
                Title="Stock" 
                Header="false" 
                Height="600" Cls="gridFont"> 
              
                <ColumnModel ID="ColumnModel1" runat="server">
                    <Columns>
                        <ext:Column ColumnID="Item_Name" Header="Item Name" DataIndex="Item_Name" Width="250"> </ext:Column> 
                        <ext:Column ColumnID="openingstock" Header="Opening Stock" Width="130" DataIndex="openingstock" Align="Right">
                        <Renderer Fn="Ext.util.Format.numberRenderer('0000.00')" />
                         </ext:Column>  
                    </Columns>
                </ColumnModel>
    </ext:GridPanel>
    its working fine.But i don't want to round the values.For eg: the value is 100.507,I want to get it as a 100.50.If the value is 100.5 i want to get it as a 100.50

    100.507=100.50
    100.5=100.50

    and also i used this example

    protected void Page_Load(object sender, EventArgs e)
            {
                string format1 = "0.00";
                string format2 = "&0,0.00";
                string myRenderer = "return Ext.util.Format.number(value,'" + format1 + "') + '</br>' + " + "Ext.util.Format.number(value,'" + format2 + "');";
                this.GridPanel1.ColumnModel.Columns.Add(new Column()
                {
                    Header = "Test",
                    DataIndex = "test",
                    Renderer =
                    {
                        Handler = myRenderer
                    }
                });
                if (!X.IsAjaxRequest)
                {
                    Store store = this.GridPanel1.GetStore();
                    store.DataSource = new object[]
                {
                    new object[] { 100.507 },
                    new object[] { 100000.507 },
                    new object[] { 1234124.234234 },
                };
                    store.DataBind();
                }
            }
    same problem i am facing its rounding the value.
    Last edited by NishaLijo; Jun 15, 2011 at 5:12 AM.
  2. #2
    dude,

    rule of thumb, always do deep research before re-inventing the wheel!

    use ext:NumberColumn instead of ext:Column.

    ext:NumberColumn have property called Format:

    A formatting string as used by Ext.util.Format.number to format a numeric value for this Column (defaults to '0,000.00'). Formats the number according to the format string. examples (123456.789): 0 - (123456) show only digits, no precision 0.00 - (123456.78) show only digits, 2 precision 0.0000 - (123456.7890) show only digits, 4 precision 0,000 - (123,456) show comma and digits, no precision 0,000.00 - (123,456.78) show comma and digits, 2 precision 0,0.00 - (123,456.78) shortcut method, show comma and digits, 2 precision To reverse the grouping (,) and decimal (.) for international numbers, add /i to the end. For example: 0.000,00/i
    hope this help!
  3. #3

    How we can format the double value to two decimal places

    Hi,

    Its not working.My requirement is no need of round off the value.

    For eg:if user enter the value as 15.256 this should be getting as 15.25 for two decimal places.
  4. #4
    I believe the number column with precision of 2 (0.00 - (123456.78) show only digits, 2 precision) will sort the problem, note in the quote above that the number is 123456.789 and via a format of 0.00 you would get 123456.78 - is that not what you want?
  5. #5
    Quote Originally Posted by NishaLijo View Post
    Hi,

    Its not working.My requirement is no need of round off the value.

    For eg:if user enter the value as 15.256 this should be getting as 15.25 for two decimal places.
    ok, at least you will always get 2 decimal digits [i.e., 1 -> 1.00]. so to fix the rounding why don't you do it in the code behind (or in JS [not recommended]). in C# the ToString() method got tons of formatting styles. so after getting that return the string back to long,int if needed.
  6. #6

    How we can format the double value to two decimal places

    Quote Originally Posted by DougMcDonald View Post
    I believe the number column with precision of 2 (0.00 - (123456.78) show only digits, 2 precision) will sort the problem, note in the quote above that the number is 123456.789 and via a format of 0.00 you would get 123456.78 - is that not what you want?

    Hi,

    Yes i want it that way.Give me the answer please....
  7. #7
    in C#
    var x= Math.Truncate(2.22977777 * 100) / 100; //Returns 2.22
    
    //in case you want it as string:
    string s = string.Format("{0:N2", x);
  8. #8

    How we can format the double value to two decimal places

    Hi,
    u have any solution in render function?I can't use it this way

    var x= Math.Truncate(2.22977777 * 100) / 100; //Returns 2.22
     
    //in case you want it as string:
    string s = string.Format("{0:N2", x);
    .

    please give me the answer.
  9. #9

    formatting

    Double d = 100.123456;
    Double dc = Math.Round((Double)d, 2);

    More about.....formatting

    Johnson

Similar Threads

  1. Replies: 2
    Last Post: Aug 04, 2011, 2:14 PM
  2. How to format a number with 3 decimal
    By NishaLijo in forum 1.x Help
    Replies: 1
    Last Post: Nov 24, 2010, 5:44 AM
  3. Replies: 4
    Last Post: Nov 22, 2010, 12:54 PM
  4. How to Show Data In Decimal format in GridColumn
    By Dinesh.T in forum 1.x Help
    Replies: 0
    Last Post: Jun 11, 2010, 7:25 AM
  5. [CLOSED] How to format a decimal number...
    By iansriley in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: May 26, 2009, 3:48 AM

Posting Permissions