|
Hi codeproject World !
How can i convert string to decimal and remove unused Zero .
example :
string decimal = "192.0205000";
Decimal D = Decimal.Parse(decimal);
and wanna to have :
D = 192.0205
NOT D= 192.0205000
PLZ HELP !
|
|
|
|
|
And what is this code doing? Did even tried this?
|
|
|
|
|
Yes
it returns :
D= 192.0205000
BUT I wanna
<code>D= 192.0205 ====> Without unUsed Zeros</code>
|
|
|
|
|
Either strip the zeros off the string or use Math.Round
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
JYIS, they aren't there! They don't exist!
Edit: I posted that before I saw that you were using Decimal rather than Double.
ToString:
"
The return value is formatted with the general numeric format specifier ("G"), and the NumberFormatInfo object for the current culture.
"
G:
"
The exception to the preceding rule is if the number is a Decimal and the precision specifier is omitted. In that case, fixed-point notation is always used and trailing zeroes are preserved.
"
modified on Wednesday, April 28, 2010 9:38 PM
|
|
|
|
|
PIEBALDconsult wrote: JYIS
What does that mean?
PIEBALDconsult wrote: they aren't there! They don't exist!
The trailing zeroes? Yes they do. See here.
|
|
|
|
|
decimal d= decimal.Parse("1120.00120000",System.Globalization.NumberStyles.AllowDecimalPoint);
|
|
|
|
|
Just a minor point - in numeric terms, 1120.00120000 is exactly the same as 1120.0012. If you're doing a calculation using a decimal then it doesn't make much sense to round trim it.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Indeed, though there is a difference in some situations, such as when using ToString(). Try running this:
string strD = "192.0205000";
Decimal D = Decimal.Parse(strD);
MessageBox.Show(D.ToString());
I never knew that the Decimal data type actually maintains the trailing zeroes.
|
|
|
|
|
|
Excellent question! And thanks for providing the answer. I had no idea the Decimal data type stores trailing zeroes.
jojoba2011 wrote: PLZ HELP !
Just a bit of friendly advice... avoid using uppercase when not necessary, don't beg for help, and don't shorten words (e.g., "PLZ") except for common acronyms. To many people here at Code Project, it appears unprofessional. Other than that, though, great question.
EDIT: It appears your answer is incorrect.
|
|
|
|
|
This is merely a quirk of the "G" format; when ToString ing a Decimal, use something along the lines of d.ToString ( "#.###############" ) .
|
|
|
|
|
Good call. One can also use:
d.ToString("G29");
|
|
|
|
|
That's basically what the default is and I don't think it works.
|
|
|
|
|
|
|
After reading what everybody has said, I think you want this:
Decimal d = Decimal.Parse(Decimal.Parse("192.0205000").ToString("G29"));
|
|
|
|
|
Hello!
I want to do some custom drawing on my windows forms application, which involves connecting my own defined controls with lines.
That should be easy, but I couldn't figure a way to draw these lines as objects/controls, so I can define events for them. I am also thinking of not getting too greedy with the memory.
I appreciate any suggestion.
Thank you!
|
|
|
|
|
This[^] might be a good beginning.
|
|
|
|
|
Here's a line control that I wrote. Real simple, and it has a LineClicked event.
marois-consulting.com/files/LineControl.zip
Everything makes sense in someone's mind
|
|
|
|
|
I have a text box and a crystal report viewer in a windows form.
As user types in some text in text box, it should immediately display in report viewer. I have done this, but I have problem here, that is "If user types too quickly then refreshing of the preview screen is fine, but if user type slowly then text is lost"
I am not able to find a solution to this problem.
I tried with multithreading, with timer but could not able to implement it.
can someone please help me in this?
Thanks in advance
|
|
|
|
|
What? The text in the TextBox? Are you clearing it? Why?
At any rate, I don't think I would implement it that way. I would have the user enter the text and then press a Button to get the preview. I find producing unnecessary previews and such to be wasteful and irritating.
|
|
|
|
|
Mahesh_Blr wrote: "If user types too quickly then refreshing of the preview screen is fine, but if user type slowly then text is lost"
Can you not refresh your report in the text box's keyup/keydown event?
|
|
|
|
|
In my program, I have an array of numbers (total number of modules that can be connected to a tracker) and an array of sums (number of modules that has to be mounted on a roof surface).
Let's suppose:
int[] intConnectables = {24, 22, 20, 16, 12, 11, 10, 7, 6}
int[] intRoofSurfaces = {43, 38, 27, 11}
The script has to group the connectables so that every number is used and there are the groups so every total adds up. If there exists a valid combination, I put the system in a seperate set (in the end that set would contain all valid combinations).
e.g.
24+12+7 = 43 | 22+10+6 = 38 | 20+16 = 27 | 11 = 11
Afterwards, I'll have to know wich element of Connectables belongs to wich group (e.g. 22 belongs to the 2nd group wich adds up to 38).
Any ideas?
Thanks in advance!
modified on Wednesday, April 28, 2010 11:05 AM
|
|
|
|
|
Are you looking for the algorithm (subset sum?) or a way to get the group number back from an element (maybe use parallel arrays, or a dictionary if you're going to move the elements)?
Or something else?
|
|
|
|