C# Count decimal places without considering the trailing zeros [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
For a given decimal with precision of 8 like 0.00010000, I would like to get the decimal places without the trailing zeros.
All methods that I tested returned 8, and in this case should be 4.

You can use string value=decimalValue.ToString("0.########") this will convert the value into a string and show up to 8 decimal places, without trailing zeros. Eg. 1.05000000 - > 1.05, 1.00000001->1.00000001. Then value.Split('.').ToList().ElementAt(1).Length

string output = Regex.Replace("0.00010000", #"[0]*$", "");

decimal someDecimal = 0.00010000000m;
Console.WriteLine(someDecimal.ToString().TrimEnd('0'));

Related

Left-pad a floating point number without losing trailing zeros after decimal point [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am attempting to write out to a fixed format text file and am having some trouble formatting the numbers properly. It needs to be padded with leading zeros and an implied decimal.
Example:
43.80
The output would need to be:
0000004380
So far, I have attempted to convert the double to a string, replace the decimal, and then pad with "0".
((amount.ToString()).Replace(".","")).PadLeft(10, '0')
The problem with this is when the number ends with zeros. The above example comes out as:
0000000438
Any suggestions?
decimal value = 34.80M;
int intVal = (int)(value * 100);
return intVal.ToString("0000000000");
You could try:
((string.Format("{0:0.00}", amount)).Replace(".","")).PadLeft(10, '0')
That way you don't lose the 2nd decimal place when the value is 0.
You can pass a .ToString() format string in as described in this article:
https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx
Does the incoming value always have two decimal places? If so, you could multiply by 100 to get rid of the decimal, and then to .ToString("D10")
(Int)(43.80 * 100).ToString("D10")
This should do the job : $"{(int)(amount * 100):D010}"

Trim empty spaces in int array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to trim the trailing spaces at the start and end in this int array and how can I do it, also I am not getting Trim() function anywhere here. Pls suggest me.
int[] arrPCT = dtOld.AsEnumerable().Select(r => r.Field<int>("PCT")).ToArray();
int are numeric values. They don't contain spaces (or any other char). It makes no sense trying to trim them.
int[] arrPCT = dtOld.AsEnumerable().Select(r => r.Field<int>("PCT").Trim()).ToArray();
If r.Field("PCT") value is of string type.

Comma-Separated String to Double C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want convert 1,2,3,4,5,6,7,8,9,10 String to Double.
I tried Convert.ToDouble(String); and Double.Parse(String); but returned 1.0
How to convert multi comma string to double?
Thanks for help.
From the looks of your question you actually have 10 numbers not 1. Use this code:
var nums = "1,2,3,4,5,6,7,8,9,10";
var digits = nums.Split(',').Select(r => Convert.ToDouble(r)).ToArray();
// the result will be an array of doubles, also this only works with .NET 3.5 or better.
Let me know if this works for you.

Create a 6 digit sequence number in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do i create a six digit sequence number in c# ?
Is there any other way than storing a string in the database like "000000" and later on incrementing it through the last inserted value ?
Use a plain old number as a sequence, this is what your DB will provide. If you want to display it with six digits, just call: yourNumber.ToString("D6")
(see docs)

C# Convert To Decimal String Value like 0.33 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to convert this string to decimal 0.55 exactly.
Decimal.parse("0,55")
I need to get exact value excluding leading zeroes.
This should fix your problem (if I understand the problem, that is):
var number = decimal.Parse("0,55".Replace(',', '.'), CultureInfo.InvariantCulture);
EDIT
Not every culture uses the point (.) symbol as the decimal separator. If you don't specify a format provider, the framework defaults to the current culture. I'm guessing that in this particular case, the decimal.Parse() method was interpreting "0,55" as decimal 55.0.

Categories

Resources