tags:

views:

156

answers:

8

I don't want to do any rounding, straight up, "39%".

So "9.99%" should become "9%".

+1  A: 
Console.WriteLine("{0}%", (int)39.9983);
Blindy
While this does work, it doesn't take into account the percent sign.
AndyPerfect
The OP question is barely legible as it is.. I'll leave this for posterity <.<
Blindy
+3  A: 

Probably a Regex. I'm no master of regular expressions, I generally avoid them like the plague, but this seems to work.

string num = "39.988%";
string newNum = Regex.Replace(num, @"\.([0-9]+)%", "%");
Anthony Pegram
Why would you do that with regex? Just remove the '%' char, parse the integer, truncate it, then ToString and add the '%'. It would be far more obvious to a reader what you're doing. Or if you insist on making it a pure string operation, just take an appropriate substring.
Joren
Sure, you could substring and concatenate the %. But you're looking at 3 operations (IndexOf!). You could remove the % sign, parse, convert back to string, and concatenate the % back. 4 operations. So, yeah, a Regex felt appropriate.
Anthony Pegram
Fewer operations doesn't make the code more readable (otherwise, we'd all be doing code golfing). Looking at your regex, I can't tell immediately what it does, where as by looking at @Ranhiru's solution below, I can see it right off the bat.
musicfreak
I respect that opinion. At the same time, most code is imcomprehensible if you are unfamiliar with the constructs used. If you are more comfortable with a multistep solution, go with it! Like I said in the answer, I'm not a big fan of regular expressions, myself. But this seemed like a perfectly good place to think about using one.
Anthony Pegram
Regex is appropriate only for the case of right string format. If somebody will pass "39.sdfsd23%" to your method in some class library, then it's going to be hard to debug. At least if we take into consideration regex you provided as an answer. It's hard to realize exception handling here.
MAKKAM
Actually, I prefer this one since you're going string to string, no need to parse it or format it afterwards. And regexes are plenty readable to those who know regexes (if you're really concerned, appending `// Remove fractional part of percentage` should be more than enough to clarify). @Anthony, if you can guarantee the string is of that format, I'd just use `Regex.Replace(num, @"\..$", "%")`.
paxdiablo
@MAKKAM, that's also going to cause grief for `Parse` :-) Question stated the format, no indication that it would be passing anything else. While your comment is correct, it appears irrelevant in this context.
paxdiablo
+5  A: 
string myPercentage = "48.8983%";

string newString = myPercentage.Replace("%","");

int newNumber = (int)Math.Truncate(Double.Parse(newString));

Console.WriteLine(newNumber + "%");

There maybe hundred ways of doing this and this is just one :)

Ranhiru Cooray
+2  A: 

One way to do it:

"39.999%".Split(new char[] { '.' })[0] + "%";
InSane
+1 for remembering the % sign.
Val
Note: Split accepts `param char[] array`, so you don't *actually* need to *explicitly* provide an array. See: @Nathan's answer. You can provide a zero-to-many `char` inputs, comma-delimited.
Anthony Pegram
+1  A: 
int.Parse("39.999%".Split('.')[0])

Doing this gives you a nice int that you can work with as you see fit. You can stick a % sign on the end with whatever string concatenation floats your boat.

Nathan Tomkins
A: 

I'm guessing you want a string returned? Probably the laziest way to do it:

    string mynum = "39.999%"
    mynum = mynum.substring(0, mynum.IndexOf('.'));
    mynum += "%";
    

To get an int, you could cast the result of line 2.

lazo
+2  A: 

Now we have two questions asking the same thing..

Heres my crack at it.

"39.9983%".Split('.')[0] + "%";
InSane
+6  A: 

Hello ,

I hope this will work.

string str = "39.999%";

string[] Output = str.Split('.');

Output[0] will have your Answer.

Thanks

PrateekSaluja