5

Suppose that we have stringvalue=125.32600 when it convert to decimal value with this code

decimal d;
decimal.tryparse(stringvalue,out d)

d value is 125.326 how can I do this convert with final result 125.32600

0

5 Answers 5

8

You cannot because 125.32600 is equal to 125.326. In this case however I guess that you want to print it out with specific format, which can be done like this:

Console.WriteLine(d.ToString("f5"));

Read Standard Numeric Format Strings

UPDATE

Extension method:

public string Format(this decimal source, int precision)
{
    if (precision < 0)
    {
        throw new ArgumentOutOfRangeException("Precision must be a non negative integer");
    }

    return source.ToString("f" + precision);
}

which can be used like this:

Console.WriteLine(d.Format(5));
Sign up to request clarification or add additional context in comments.

8 Comments

yeh, exactly. but number of trailing zero(s) depends on user define setting. I Think I have to store decimal point numbers as a option
f5 in this case means 5 numbers after decimal point. If you replace it with f3 then it will show only 3 digits after decimal point.
So you can have precision variable and format your value as you wish.
@walkhard: because "f5" only works with that specific value; no attempt made to provide a generic solution. (btw. also turns out your answer suffers from the same flaw as mine :))
@ErikAllik No such thing was requested, besides OP can easily rewrite it as needed, but since you've mentioned it I've updated my answer with generic solution.
|
3

Your code works as written (as long as the decimal separator matches your culture):

decimal d;
decimal.TryParse("125.32600", NumberStyles.Number, CultureInfo.InvariantCulture, out d);
s = d.ToString(CultureInfo.InvariantCulture); // 125.32600

Decimal already remembers how many trailing zeros it has. This is caused by decimal representing numbers in non-normalized form, with an integer mantissa and an exponent representing the number of decimal digits. e.g. 125.32600 is represented as 12532600 * 10^-5

Comments

2

The answer is: You can't, at least not like that.

EDIT: correction: decimal already works like that; but you'll still find below a useful way to store your decimals in a DB.

Why? Because that's not how decimals are stored in memory.

Solution: if you need to keep the trailing zeros, just remember the precision explicitly in a separate field (of a class you should create for this purpose); or store the decimals in string form and only convert to decimal as needed.

string strValue = "125.32600";
int precision = strValue.Length - 1;  // only the "12332600" part
decimal value = Decimal.Parse(strValue);

stores 8 in precision and 125.326 in value.

To get back the original form:

int afterPt = precision - ((int) value).ToString().Length;
Console.WriteLine(value.ToString("f" + afterPt));

prints

125.32600

P.S. you have to be aware of floating point binary representation issues though, so stuff like 4.05 might be stored as e.g. 4.049999999999999999, so if you need to guarantee this won't happen, use an algorithm that bypasses decimal altogether and uses only integers for storage and computation.

string strValue = "125.32600";

// parse and store
int value = int.Parse(strValue.Replace(".", ""));
int periodIx = strValue.IndexOf(".");

// get back the original representation
string str = value.ToString();
Console.WriteLine(str.Substring(0, periodIx) + "." + str.Substring(periodIx, str.Length - periodIx));

NOTE: Make sure to use , instead of . in locales that need it.

9 Comments

Thank for your response. but I want store user input value with trailing zero(s) in database, what is your solution?
Store the string, or store a pair of values: precision and value where precision stores the number of significant decimal places and value is a normal decimal.
@user2377017: I've amended my answer with the necessary details.
Your answer would be true for double, but it's wrong for decimal in two ways: 1) stuff 4.05m is exactly representable as decimal 2) decimal doesn't require normalization, it can distinguish 1.0 and 1.00.
OK, but he still needs to store it in the database.
|
1

What you can do is count the zeroes in string and then store them in separate DB field. When you want the result with zeroes just concatenate the same no. of zeroes into decimal number string.

ex.

string p="123.456000";
int zeroes=p.Split('0').Length - 1; // guess
decimal value = Decimal.Parse(p); //without zeroes
string valWithZero=value.toString().padRight(zeroes,'0'); //with zeroes

Comments

0

If you really want to have the zeros in the database you could save it as a string, preformatted, but that would be very inefficient.

What is the problem you try to solve by this, there might be a better solution?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.