473,757 Members | 10,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decimal.Parse and Decimal.ToStrin g Methods

Dim value As Decimal = 1234.56
Dim format As String = _
"Positive $#,##0.00;Negat ive $#,##0.00;"

Dim s As String = value.ToString( format)
Dim d As Decimal = Decimal.Parse(s )

The last line above throws an exception because it can't parse "Positive
$1,234.56". It can convert to that format, but how can one go about parsing
it using the same format that was used to format it as a string? Is there
an easy way w/o too much work?

Thanks,
Mythran
Nov 21 '05 #1
1 7503
Mythran,

There is probably a more elegant way, but this might work for you:

Dim value As Decimal = 1234.56
Dim s As String = value.ToString( "Negative $#,##0.00;Negat ive
$#,##0.00;")
Dim s2 As String
Dim d As Decimal

s2 = s.Replace("Posi tive ", "")
s2 = s2.Replace("Neg ative ", "-")
d = Decimal.Parse(s 2, System.Globaliz ation.NumberSty les.Currency)

MsgBox(s)
MsgBox(d)

Kerry Moorman
"Mythran" wrote:
Dim value As Decimal = 1234.56
Dim format As String = _
"Positive $#,##0.00;Negat ive $#,##0.00;"

Dim s As String = value.ToString( format)
Dim d As Decimal = Decimal.Parse(s )

The last line above throws an exception because it can't parse "Positive
$1,234.56". It can convert to that format, but how can one go about parsing
it using the same format that was used to format it as a string? Is there
an easy way w/o too much work?

Thanks,
Mythran

Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3026
by: BA | last post by:
Hello, I have a string with a price in: "$14.95" I need to get it into a decimal. So I regex 'd the string and dumped the $. Debug mon shows a clean string "14.95" Then I do a System.Convert.ToDecimal on the clean string and the output
3
24402
by: Mark | last post by:
How do you parse a currency string to a decimal? I'd like to avoid having to parse the number out, removing the $ manually. That sounds like a hack. There are times that this string will be currency and others when it will be a text integer or decimal. //This bombs because of the string having an improper format. Decimal.Parse("$9,200.00") Thanks in advance! Mark
2
6507
by: Piotr | last post by:
Hi I'm working with Datagrid containg decimal (editable) numbers. The user-entered numbers are read by decimal.Parse() method. However, Datagrid formats some decimals as "1", but some as (eg) "1.00" - depends on what the user had entered. The page has DISABLED viewstate. So, let someone explain me why the number 1 is not the same as 1.0 or 1.000? I cannot find any property to set the precision.
3
12922
by: JenHu | last post by:
Hi all, I have to read from a text file and generate values and insert to database. But first of all, when the text file contains '0000000000', I received a sEfundAmt value = 0D instead of 0.0 on sEfundAmt = Decimal.Parse(Mid$(sRetLine, 30, 10)) / 100.0 For example, in the text file, value is '0000150776', I want to
3
2301
by: Stefan Richter | last post by:
Following problem: On a german computer system I am developing a program for international computer systems, and I want my programm always to run in the same way. There's a difference in numbers, we use "," instead of "." and "." instead of ",". (Don't know why!!!) The method decimal.parse tries to handle that, so when you are using it on a german system,
13
9247
by: Jason | last post by:
Could someone here show me how I would write a vb program to convert decimal ip address to binary? For example a small form with a convert button and a label for the result and a textbox for the ip. So I would want 11000000 10101000 00000010 00001010 tp show up inthe label if I entered 192.168.2.10 into the text box. I have no idea even how to begin this, any help would be great.
8
6143
by: Rico | last post by:
Hello Everyone, I observed something strange in some quick testing code: void Button1Click(object sender, System.EventArgs e) { CultureInfo culture = new CultureInfo("th-TH", false); tbxDisplayResult.Text = DateTime.Parse(tbxInputArea.Text, culture).ToString("yyyy MMMMMMMMMM dd");
4
4414
by: =?Utf-8?B?cGF0cmlja2RyZA==?= | last post by:
Hi everyone! I'm using greece - greek in my control panel's regional options, and so, my decimal point is the comma (,), while it is the dot (.) for the sql server db, however, I'm facing trouble when I need to parse the sql server decimal as an asp.net double! For example:
9
2973
by: jpoe | last post by:
I have adopted this Code from an application built by consultants. My company needs to now pass decimal values for the 'discountRate' variable below. Using the debugger I receive and the program passes a correct integer to a webservice but when the database contains a decimal value a '0' value is received. I thought the data type (originally Int64) for the variable assignment was the problem but changing to 'decimal', 'double', or 'float' does...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.