You can extend your number to "10 ^N" times ("N" means the digit number). And the you can follow this sample to implement your reality:
namespace CSharp
{
classProgram
{
///<summary>/// Auto return you a number without random pricing///</summary>///<param name="num">The original number</param>///<param name="p">The digit that you wanna step in</param>///<returns>A double number</returns>staticdouble AutoDigit(double num,int p)
{
int n = 10;
for (int i = 1; i < p; i++)
{
n *= 10;
}
num = num * n;
Console.WriteLine("Num is"+num);
double t = (int)num;
return t / n;
}
staticvoid Main(string[] args)
{
Console.WriteLine(AutoDigit(12345678.8888,2));
}
}
}
For web, you can first make it "protected":
/// <summary>
/// Auto return you a number without random pricing
/// </summary>
/// <param name="num">The original number</param>
/// <param name="p">The digit that you wanna step in</param>
/// <returns>A double number</returns>
protected double AutoDigit(double num,int p)
{
int n = 10;
for (int i = 1; i < p; i++)
{
n *= 10;
}
num = num * n;
Console.WriteLine("Num is"+num);
double t = (int)num;
return t / n;
}
protected double AutoDigit(double num, int p)
{
int n = 10;
for (int i = 1; i < p; i++)
{
n *= 10;
}
num = num * n;
Console.WriteLine("Num is" + num);
double t = (int)num;
return t / n;
}
The number i used was 12345678.88
However my database value is FLOAT field what do i do 12345679 is the number being displayed on using the above code
protected double AutoDigit(double num, int p)
{
if((int)num==num)
{
return num;
}
int n = 10;
for (int i = 1; i < p; i++)
{
n *= 10;
}
num = num * n;
Console.WriteLine("Num is" + num);
double t = (int)num;
return t / n;
}
protectedobject AutoDigit(object num, int p)
{
double dnum = Double.Parse(num.ToString());
int inum = (int)dnum;
if (inum== dnum)
{
return num;
}
int n = 10;
for (int i = 1; i < p; i++)
{
n *= 10;
}
dnum = dnum * n;
int t = (int)dnum;
return t*1.0 / n;
}
vinikatyal
Member
22 Points
30 Posts
Show Amount upto two decimal places without rounding off in DataGrid
Dec 08, 2012 11:43 AM|LINK
Say for example i have a large number 12345678.88 right now when i display it in the Datagrid
with the use of
<asp:Label ID="txtlineAmt" runat="server" ReadOnly="true" Text='<%# Eval("DtaLineAmount", "{0:F}") %>'></asp:Label>This number is getting display as 12345680.00 How do i display it as it is 12345678.88 even if the number becomes 12345678.88888
I saw the below code somewhere to solve my issue but don't know how to use it
result=string.Format("{0:0.0}",Math.Truncate(value*10)/10);oned_gk
All-Star
31237 Points
6384 Posts
Re: Show Amount upto two decimal places without rounding off in DataGrid
Dec 08, 2012 12:30 PM|LINK
Text='<%# Eval("DtaLineAmount", "{0:#,##0.00}") %>'Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Show Amount upto two decimal places without rounding off in DataGrid
Dec 09, 2012 02:54 AM|LINK
Hi,
You can extend your number to "10 ^N" times ("N" means the digit number). And the you can follow this sample to implement your reality:
For web, you can first make it "protected":
/// <summary> /// Auto return you a number without random pricing /// </summary> /// <param name="num">The original number</param> /// <param name="p">The digit that you wanna step in</param> /// <returns>A double number</returns> protected double AutoDigit(double num,int p) { int n = 10; for (int i = 1; i < p; i++) { n *= 10; } num = num * n; Console.WriteLine("Num is"+num); double t = (int)num; return t / n; }And then refer that function:
<%#AutoDigit(Eval("ColumnName"),2)%>
vinikatyal
Member
22 Points
30 Posts
Re: Show Amount upto two decimal places without rounding off in DataGrid
Dec 09, 2012 03:47 AM|LINK
Doesn't work
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Show Amount upto two decimal places without rounding off in DataGrid
Dec 09, 2012 04:05 AM|LINK
But it works on me very well……So what's actually happening there?
vinikatyal
Member
22 Points
30 Posts
Re: Show Amount upto two decimal places without rounding off in DataGrid
Dec 09, 2012 04:31 AM|LINK
Text= '<%#AutoDigit(Convert.ToDouble(Eval("DtaLineAmount")),2)%>'I tried this using the code you mentioned :-
protected double AutoDigit(double num, int p) { int n = 10; for (int i = 1; i < p; i++) { n *= 10; } num = num * n; Console.WriteLine("Num is" + num); double t = (int)num; return t / n; }The number i used was 12345678.88
However my database value is FLOAT field what do i do 12345679 is the number being displayed on using the above code
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Show Amount upto two decimal places without rounding off in DataGrid
Dec 09, 2012 05:40 AM|LINK
Well……Please do a little change:D
protected double AutoDigit(double num, int p) { if((int)num==num) { return num; } int n = 10; for (int i = 1; i < p; i++) { n *= 10; } num = num * n; Console.WriteLine("Num is" + num); double t = (int)num; return t / n; }hafizzeeshan
Participant
834 Points
253 Posts
Re: Show Amount upto two decimal places without rounding off in DataGrid
Dec 09, 2012 07:48 AM|LINK
Hi
1) Math.Round(Convert.ToDecimal(your value), 2);
2) <asp:Label Text='<%# String.Format("{0:f2}", DataBinder.Eval(Container.DataItem,"testfield")) %>' ...../>
Zeeshan Rauf
# 92-3216698206
Email : zeeshan_rouf@hotmail.com
Please mark the replies as answers if they help or unmark if not.
vinikatyal
Member
22 Points
30 Posts
Re: Show Amount upto two decimal places without rounding off in DataGrid
Dec 09, 2012 09:32 AM|LINK
@Decker
I still get the same result No success!!
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Show Amount upto two decimal places without rounding off in DataGrid
Dec 10, 2012 12:51 AM|LINK
Sorry but I cannot reproduce your issue, and everything goes with me quite fine.
And please hope next time please don't use "!", which isn't polite to others;)
Now I'll give you this sample:
[aspx]
[cs]