Jun 15, 2011, 5:01 AM
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.
100.507=100.50
100.5=100.50
and also i used this example
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.50100.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.