wyrd Posted May 9, 2003 Posted May 9, 2003 Using the currency string format; string.Format("{0:C}", total) how can I display negatives properly? In this format is shows a negative currency like this; ($32.53) ... or is that the proper way to show negative money values? I'm looking for something more like this; -$32.53 Thanks in advance. Quote Gamer extraordinaire. Programmer wannabe.
hog Posted May 9, 2003 Posted May 9, 2003 try: Dim dblValue as Double = -32.53 MessageBox.Show(Format(dblValue,"$0.00")) Quote My website
wyrd Posted May 9, 2003 Author Posted May 9, 2003 Do you know if that's a VB only function? I can't seem to find the function anywhere (using C#). If not, do you know what Namespace it's under in the .NET framework? Quote Gamer extraordinaire. Programmer wannabe.
*Gurus* divil Posted May 9, 2003 *Gurus* Posted May 9, 2003 That's a VB function. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
wyrd Posted May 9, 2003 Author Posted May 9, 2003 :eek: Is there a similar function in C#? Or am I stuck having to create my own? :( Quote Gamer extraordinaire. Programmer wannabe.
*Experts* Nerseus Posted May 9, 2003 *Experts* Posted May 9, 2003 You can use ToString("c") with a new NumberFormatInfo object, ToString("$0.00"), or another variant of ToString with a specific format. For example: double d = -123456789.1234; NumberFormatInfo formatter = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone(); formatter.CurrencyNegativePattern = 1; Debug.WriteLine(d.ToString("c", formatter)); Debug.WriteLine(d.ToString("$###,###,##0.00")); Debug.WriteLine(d.ToString("$0.00")); This prints: -$123,456,789.12 -$123,456,789.12 -$123456789.12 The reason you get parens on your negative number is that the default number provider is from InvariantInfo. To override that, you need to create your own NumberFormatInfo object. In the code above, I clone the InvariantInfo and change the negative pattern from 0 to 1 (check the help for other formats). There's probably a way to get the setting from the user's machine, but I'm just not sure :) -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
wyrd Posted May 9, 2003 Author Posted May 9, 2003 Interesting.. I'll check out the help files on this, thanks. Quote Gamer extraordinaire. Programmer wannabe.
hog Posted May 10, 2003 Posted May 10, 2003 Mmm is it me or is C# making life a tad difficult? Sorry wyrd didn't know you were using C# Quote My website
*Gurus* Derek Stone Posted May 10, 2003 *Gurus* Posted May 10, 2003 Mmm is it me or is C# making life a tad difficult? Or is Visual Basic .NET making life a tad bit too easy? Quote Posting Guidelines
hog Posted May 10, 2003 Posted May 10, 2003 Come come, surely that is how it should be? I remember when I used to program in Borland C++ and had to create Windows forms from the ground up. I felt like yeah I'm a programmer I can do this tricky stuff. When I moved to VB5 I felt I was cheating as hey presto intant Windows forms minimal effort. It took me a while to get over that feeling, but now I agree with what many authors say in the books I've read. Why waste time doing mundane stuff when you can call a function, method or whatever that someone else has already created? Better to concentrate on what you want you application to be doing than faff about rewriting the Format function. Not having a pop here Derek, I'm a firm believer in each to his own, but if VB is making life too easy then are you saying you would rather code everything to ensure any application you write was done so with maximum effort right the way down to writing your own keyboard interupt handler? Quote My website
wyrd Posted May 10, 2003 Author Posted May 10, 2003 Come come, surely that is how it should be? Not always. Why waste time doing mundane stuff when you can call a function, method or whatever that someone else has already created? Authors generally say this because; - They're trying to sell their book. - They weren't smart enough to become a guru in C/C++ (I include myself here) - They're lazy and like having work done for them. But in all seriousness, in the case of RAD, we're pretty much stuck with Microsoft. You need to realize that MS adds a ton of error checking code, which tends to add a load of bloating and overhead to their classes in general. If you're new to programming, this is a good thing. But if you're not, all it does is slows down your program needlessly. Better to concentrate on what you want you application to be doing than faff about rewriting the Format function. Formatting text is part of what an application does, I'd think it'd be something you'd like to worry about. Not having a pop here Derek, I'm a firm believer in each to his own, but if VB is making life too easy then are you saying you would rather code everything to ensure any application you write was done so with maximum effort right the way down to writing your own keyboard interupt handler? I don't think that's what he's saying at all. Derek seems to be like me, in that I'd like to know exactly what my application is doing and why. By adding functions for everything so you never, ever know what's going on, and thus making it insanely easy, is a bad thing IMO. It's like your mom making you food every night when you're a kid. Eventually when you grow you'll need to learn how to cook for yourself, otherwise you'll end up starving to death. Quote Gamer extraordinaire. Programmer wannabe.
hog Posted May 10, 2003 Posted May 10, 2003 Nah, have to disagree on this one. This falls into the same argument re Windows V Linux, you're either one or t'other. Formatting text is part of what an application does, I'd think it'd be something you'd like to worry about. The mention the Format function was an example. I mean, why bother wasting time writing your own Format function when there is already one there that fits the purpose? A bit like why re invent the wheel? Any way we have two schools of thought here, both of which can be viewed a right. I get where you are coming from when you want to get every last bit of performance, but for the average application as far as the ones I write this is not an issue. In my line writing a custom Format, IsNumeric etc just ain't worth the effort :-) Quote My website
wyrd Posted May 10, 2003 Author Posted May 10, 2003 At the cost of alienating my own thread, this will be the last reply on the whole "ease of use" subject. If you want to argue further I'd be more then happy to, just create a thread for it in the Random Thoughts forum. :) This falls into the same argument re Windows V Linux, you're either one or t'other. It's not, actually. In the case of programming, you use the best tool for the job. If you want to do some programming at the hardware level, you certainly can't use .NET. I get where you are coming from when you want to get every last bit of performance I never said I did, I was using it as an example as to why you should always know exactly what something is doing and why. Certainly in business applications raw speed doesn't matter to much (of course there are always situations where it does), so long as the database it's built on is efficiently designed. Quote Gamer extraordinaire. Programmer wannabe.
hog Posted May 10, 2003 Posted May 10, 2003 Nuff said apart from who said I was arguing? Not why I use this site....we all have our own views, being a boring world if we all thought the same. :-)...........end Quote My website
wyrd Posted May 10, 2003 Author Posted May 10, 2003 (edited) Okay back to my topic.. I found an easy way, and yet again something new which I didn't know before. Hopefully someone else will find this useful as well. When formatting, you can specify more then one format; {format 1;format 2} or {format 1;format 2;format 3} What does this mean? When looking at the number to format, it applies the format based on; {greater than or equal;less than} or {greater than;less than;equal to} You can also apply formats in the ToString() method of any number type. Example: double d = 234.43; double d2 = -323.34; double d3 = 0; // Note; you can apply the same logic to String.Format() d.ToString("$###,###,##0.00;-$###,###,##0.00"); // This would display $234.43 d2.ToString("$###,###,##0.00;-$###,###,##0.00"); // This would display -$323.34 d3.ToString("$###,###,##0.00;-$###,###,##0.00;$0.00"); // This would display $0.00 (note: so would the format used in d.ToString()) Also, it's aparently more efficient to use the ToString() method for formatting when possible rather then String.Format(). Edited May 10, 2003 by wyrd Quote Gamer extraordinaire. Programmer wannabe.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.