Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I got this method trying to remove decimal

C#
public static int[] encode(String orig)

       {

           String int_part = " ";

           String dec_part = " ";

           Regex digitsOnly = new Regex(@"[^(\d|\.)]");

           //orig = Regex.Replace(orig,"[^\\.0-8]", ""); //remove non-numerical characters



           int dot_index = orig.Trim().IndexOf("0");



           if (dot_index > 0)

           { //do we have a decimal place?



               int_part = orig.Substring(0, dot_index); //get integer portion of string

               dec_part = orig.Substring(dot_index); //get part after decimal



               int_part = Regex.Replace(int_part, "^0+", ""); //remove leading 0's

               dec_part = Regex.Replace(dec_part, "0+$", ""); //remove trailing 0's



               int[] result = new int[2];

               int.TryParse("0", out result[0]);

               int.TryParse("1", out result[1]);



               return result;

           }

           return new int[] { 0, 1 };

       }
Posted
Updated 25-Oct-12 1:04am
v2
Comments
Street Racers 25-Oct-12 6:53am    
what is the problem?
kolisa 25-Oct-12 7:15am    
i wanna convert inputed decimal from this 1000,1 to 10001
johannesnestler 25-Oct-12 7:21am    
?? from code I assume you want to convert a string to an integer array with the digits found in the input string, ignoring decimal seperators (dot does it only for english culture, e.g in german we use comma as decimal seperator). Though I can not imaging why you want to do this - but is this what you want?
Keith Barrow 25-Oct-12 7:07am    
Hi, I added code tags to your code: it makes it much more readable, and will improve the likelihood of your getting an answer.

There is a simpler way to do this than string manipulation:

  1. Remove the non-numeric part of the number, preserving the decimal point.
  2. Use Decimal.Parse[^] (or Decimal.TryParse[^]) to convert your string into a decimal
  3. Use Decimal.Truncate[^] to get the integer part, the result of this is a decimal, so you will need cast to int if needed. You could also use Math.Floor[^] if you want
  4. Subtract the integer part from the original value & perform a ToString(). Stripping the leading "0." from this will give you the fractional part as a string


Note storing the fractional part as an int might cause problems:
7.01 --> int = 7, fratctional string = "01" if you parse 01 to an int, you'll get 1 so your array would be 7,1
7.1 --> int = 7, fratctional string = "1" if you parse 1 to an int, you'll get 1 so your array would be 7,1 also

[Edit]
Actualy I've been an idiot: once you have the "decimal" string, you can do a string .split('.') on it and parse the two elements in the resulting two-element array of string :doh:
 
Share this answer
 
v3
Not exactly know that what problem you are getting , I think you want to convert your decimal value in Int. so why not you use inbuld method like convert or parse, that might be useful for you.

Thanks,
Ambesha
 
Share this answer
 
Comments
kolisa 25-Oct-12 7:16am    
i want to convert from in input of 1000.1 to 10001 - 1
Keith Barrow 25-Oct-12 8:16am    
do you want to covert the number into a new format e.g. 3.13 to 3 - 14. If so you can use digitsOnly.Replace(".", " - ")
Ambesha 25-Oct-12 7:21am    
like you want input-> 1000.1 and it convert like 10001 - 1??
use the regex, it's very fast and simple. The Following code should solve your issue.

Regex digitsOnly = new Regex(@"[^\d]");
var test12 = digitsOnly.Replace("1000..A1", string.Empty);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900