decimal problem

G

Goh

Hello, any body can help me?

My problem is deal with the decimal value

My table field is designed to store decimal value with
Precision 20 and Scale 5

If 20.1 is a value stored in that table,
The system display it as 20.10000 in aspx page
I want the value display 20.1

This problem exists after installing the framework 1.1
It work ok when I was work with framework 1.0

My question is : is there any configuration setting that
can be set to disable this behavior?

P/S: I dont want to use the Formant function or
FormatNumber function.
 
J

Jon Skeet

Goh said:
My problem is deal with the decimal value

My table field is designed to store decimal value with
Precision 20 and Scale 5

If 20.1 is a value stored in that table,
The system display it as 20.10000 in aspx page
I want the value display 20.1

This problem exists after installing the framework 1.1
It work ok when I was work with framework 1.0

Yes - the decimal type was changed to keep track of the number of
zeros.
My question is : is there any configuration setting that
can be set to disable this behavior?

I don't believe so.

Your best bet is probably to normalize the number. I wrote some code to
do that a while ago. Here it is, with some test data:

using System;

public class Test
{
public static void Main()
{

Console.WriteLine (Normalize (123.0m));
Console.WriteLine (Normalize (1230.040m));
Console.WriteLine (Normalize (1230m));
Console.WriteLine (Normalize (123.00m));
Console.WriteLine (Normalize (1230.000m));
}


public static decimal Normalize(decimal d)
{
int[] bits = decimal.GetBits(d);

int sign = bits[3] & (1<<31);
int exp = (bits[3]>>16) & 0x1f;

uint a = (uint)bits[2]; // Top bits
uint b = (uint)bits[1]; // Middle bits
uint c = (uint)bits[0]; // Bottom bits

while (exp > 0 && ((a%5)*6 + (b%5)*6 + c)%10==0)
{
uint r;
a = DivideBy10 ((uint)0, a, out r);
b = DivideBy10 (r, b, out r);
c = DivideBy10 (r, c, out r);
exp--;
}

bits[0] = (int)c;
bits[1] = (int)b;
bits[2] = (int)a;
bits[3] = (exp<<16) | sign;
return new decimal (bits);
}

static uint DivideBy10 (uint highBits, uint lowBits,
out uint remainder)
{
ulong total = highBits;
total <<= 32;
total = total | (ulong)lowBits;

remainder = (uint)(total%10L);
return (uint)(total/10L);
}
}
 
G

Goh

thanx for ur reply.

but I am looking for is that any setting in 'web.config'
to disable the preserving of trailing zero. (The value
should be retrieve from database rather than from memory
variable)
 
J

Jon Skeet

Goh said:
but I am looking for is that any setting in 'web.config'
to disable the preserving of trailing zero.

There isn't one.
(The value should be retrieve from database rather than
from memory variable)

Retrieve it from the database into memory, and then normalize it. Or
call Normalize with the result of the database retrieval call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top