Convert string to decimal

I have strings like this: 0.8933 - 0.7733 - 1.2234

I notice that the ones that are 0.xxx and near to 1, they are round up to 1.0000 when I convert the strings to decimal.

I need them to remain the same value as decimal to insert them in the db.

How can I achieve it and avoid rounding?

Many thanks.
0
Delavegaz
5/1/2009 3:45:29 AM
asp.net.getting-started 91979 articles. 3 followers. Follow

5 Replies
584 Views

Similar Articles

[PageSpeed] 20

Use decimal.Parse. Look at the following example page:

aspx:

<asp:TextBox runat="server" ID='txtNumber'></asp:TextBox>

<asp:Button runat="server" ID='btnExecute' Text='execute'

              onclick="btnExecute_Click" /><br />

<asp:Label runat="server" ID='lblResult'></asp:Label>

C#:

decimal dec = decimal.Parse(txtNumber.Text);

lblResult.Text = dec.ToString();

 

VB.Net:

Dim dec as Decimal = Decimal.Parse(txtNumber.Text)

lblResult.Text = dec.ToString()

 

[The C# / VB.Net code goes into the handler of the click button].

You should see that even 0.00000023 retains its value, as does the numbers you stated.


All that glitters is gold-
Only shooting stars break the mold.
0
HeartattacK
5/1/2009 8:09:03 AM

Try this:  

string str = "0.8933";

Decimal d = Convert.ToDecimal(str);


Please mark it as answer if it resolves the issue.

visit: http://technicalsol.blogspot.com
0
sumitd
5/1/2009 9:12:40 AM

Thank you. Debugging, I noticed that the Convert is working ok, the round is made when the value is passed to SQL Server. The variable type in the SQL Server is decimal. 

What can it be?

0
Delavegaz
5/1/2009 12:53:32 PM

I found my solution here: http://forums.asp.net/t/960019.aspx

Many thanks

0
Delavegaz
5/1/2009 12:59:09 PM

The equivalent type in SQL is money. [Money and decimal are used for situations that require extreme levels of acuracy.] If you ue number(x,x) or similar, some rounding will still occur. Decimal and money are accurate enough for use in financial applications where very high degrees of accuracy are needed.


All that glitters is gold-
Only shooting stars break the mold.
0
HeartattacK
5/1/2009 1:15:29 PM
Reply:

Similar Artilces:

CLR.NET datatype for decimal is System.String or Decimal
CLR.NET datatype for decimal is System.String or System.Decimal the documentation says System.string while it is mapped to DbType.decimal I need some understanding on this. any help thanks vsv ...

Getting error while converting string to double: Input string was not in correct format
Select Case LocalMeasuringUnitText Case "Currency" 'do the conversion hereDim ConversionRate As Double = GetConversionRate(_LanguageID)   Dim UKValue As Double 'String.Format("{0:c}", SelectedRow(rowloop).Item("TransData").ToString()) Dim varValue As Object = NothingDim Stringvalue As Object = SelectedRow(rowloop).Item("TransData").ToString.Trim 'varValue = Double.TryParse(Stringvalue, varValue) 'CType(Stringvalue, System.Double) 'UKValue = Convert.ToDouble(Stringvalue) / GetConversionRate(ConfigurationManager.AppS...

Convert decimal to string
I need to put a decimal field into a message box. I converted it to string, and it shows about 10 decimal places. I only want the user to see 2 decimal places to the right of the decimal. Connie Phillips phillips.connie@bsc.bls.com use mid to find the decimal then left() to chop off > 2 places beyond it.. ..02 Connie Phillips wrote in message <3485C370.4886@bsc.bellsouth.net>... >I need to put a decimal field into a message box. I converted it to >string, and it shows about 10 decimal places. I only want the user to >see 2 decimal places to the right of the d...

convert string to decimal
Hi, I'm not sure how the PB function DEC interprets strings to convert them to decimals. Is the interpretation of "." and "," everytime american or dependent on some settings? TIA Chris Werner It depends on what you define as a decimal separator in your regional settings. HTH, Davy "Chris Werner" <cw[please_no_spam]@f-s.de> wrote in message news:4146a161@forums-1-dub... > Hi, > > I'm not sure how the PB function DEC interprets > strings to convert them to decimals. Is the > interpretation of ".&...

Converting classic to .NET 2 code, and .NET gets an error back from ADS?
 For future-proofing, our Internal Phone Directory is being moved onto our AD servers, and obviously we're using LDAP for queries.To get the phoneboook queries working, I adapted the current classic ASP code to use LDAP, and that works fine. However, we're wanting to move these pages to .NET, and that's where the problem comes in. Using the same basic logic from the Classic code (90% of its unchanged) to produce an LDAP query, I get the error  155 Unknown error (0x80005000)This is using the same website, and so the same credentials  - unless aspx pages run und...

Converting decimal to string
Hi, I need to convert a decimal number to a string local-formatted. For example, number 1234.56 should be converted in a string "1.234,56" when program runs with Italian international settings. The problem was born settings the initial value for a decimal dw column with formatted edit mask. Thanks. Marco In script, the String function will do that based on the Windows regional settings. On Sat, 8 Dec 2001 23:16:20 +0100, in powersoft.public.powerbuilder.powerscript Marco Sarti <marco.sarti@nospan.xx> wrote: >Hi, > >I need to convert a decim...

used a c#.net to vb.net converter and am now getting a syntax error.
ok I used a cool utility at http://www.kamalpatel.net/ConvertCSharp2VB.aspx to convert some c#.net to vb.net and it spit out a bunch of code which almost works but I am getting a syntax error on this line Imports (StreamReader sr = Shadows Function)() As StreamReader(objResponse.GetResponseStream()) I tried it that way and also like Imports (StreamReader sr = Shadows Function)() As StreamReader(objResponse.GetResponseStream()) but either way gave the same error Compiler Error Message: BC30035: Syntax error. Source Error: Line 26: Line ...

how to get start with .NET
the net help people installed  to my desktop 1-microsoft visual studio .NET enterprise developer 2003 2-microsoft .NET framework 1.1 what else do i need ...???  and how can i try with some simple code in VS.Net and run it to see wat happen...????? please help If you want to develope web applications, then you'll also need IIS on your PC.  That'll require XP-Professional or Windows 2000/2003, but XP-Home edition won't allow IIS installation. As for how to get started, you can search the web for some tutorials or buy a good book on VS.  The topic is way too vast to...

Getting Start With .net
hi i'm intersted to learn asp.net (vb or delphi) . i programming with delphi6 but i'd like to tp know something about programming in internet specially with .net . i hear about microsoft.net with delphi 7 that i can programming with delphi in .net .... is it right ? what 's our opinian about it ? and better to learn Vb.Net or continu with delphi(or delphi.net) thanks. Ali Darabian I think there is some support for Delphi in .Net. However, you will find much more support from the user community if you choose a more popular language such as VB.Net or C#. Since your al...

Where to start and how did you get started?
Hi, I have had a feeling for a long time to jump into the adds market for some time due to my low income (shooting for the stars) and I don't have anything better to do these days. But I find it really difficult not to give up due to lack of guides (what I can find atleast) and all the hard words which only makes me confused. But I won't give up. Never. Not sure if this is the right place to post this, but I deffinitly would be really greatfull for help getting started. How did you get started? +If there are any teacher that wanna show me the ropes private, I can give payment with ...

System.Net.Mail is giving error CS0029: Cannot implicitly convert type 'string' to 'System.Net.Mail.MailAddress'
I'm getting an error while trying to use the wizard to create a form and than pass it the vaibles to Mail. Any help would be much appreciated. SmtpClient smtpClient = new SmtpClient(); MailMessage message = new MailMessage(); try {   message.From = txtEmail.Text; message.To.Add("admin1@yoursite.com"); message.Subject = "Feedback"; message.CC.Add("admin1@yoursite.com"); message.CC.Add("admin2@yoursite.com"); message.IsBodyHtml = false; message.Body = txtMessage.Text; smtpClient.Send(message); lblStatus.Text = "Email successfully sent."; } catch (Exception ex) { lbl...

Converting String to Minutes in .net
hi all, i have a string value(Ex:"915"),i need to convert in to minutes(EX:15:15) please provide me the code for that. Hi, As per my calculations 915 comes to 15:25 that is 15 hr 25 min and not 15:15. If you think I am correct, here is the code:Dim hr As Integer = CInt("915") / 60 Dim min As Integer = CInt("915") Mod 60 Regards...

Getting Started with .net and a few questions
Hello I've done some asp and php programming along with various other languages in the past and slowing getting my head around .net.I have a small project of creating a Holiday Booking system on our Intranet and just trying to get the first part to work properly will hopefully get me sorted for the whole project. The Intranet is running on .net 2 and I prefer to use the VB code rather than C#. This is connecting to an SQL server.My initial page that I am trying to create is just for an admin user to view which dates in a table have been specified as Bank Holidays and be able to add ...

Getting started with JScript.NET
I'm a Javascript and Actionscript developer, and the thought of being able to do server-sides in something I know was too good to resist. Does anyone know some good tutorials for developing ASP.NET pages in JScript?  I can find lots of console code, but simple things are hard: like I can't even figure out how to print something out to the web page (just a string). What I'm really trying to do is access a database, verify a login, and print JSON out to the response.  What should I do to start?  Is there a better way to do this? (IE: using some JSON library already?). I'm commun...

Web resources about - Convert string to decimal - asp.net.getting-started

File:Convert to SVG and move to Commons.svg - Wikipedia, the free encyclopedia
As a courtesy (but not a requirement), please e-mail me or leave a note on my talk page if you use this image outside of Wikipedia. Thanks! As ...

Facebook Co-Founder Dustin Moskovitz Sells 450K Shares, Converts 7M More
The great Facebook stock sale frenzy continues on with a report that Co-Founder Dustin Moskovitz unloaded 450,000 shares in the social network ...

Facebook Announces “App2User” Liquidity Program for Merchants to Convert Rewards Points into Credits
... for Credits, just announced a new Credits program called “App2User,” designed to enable merchants and loyalty program operators to convert their ...

How to Convert Leads Into Buying Customers - The 9 Step Sales Process EVERYONE Should Use
... School of Business in Toronto on the topics of Sales and Entrepreneurship. I shared with them the sales process that I find How to Convert Leads ...

Bitcoin : Should I convert my savings to Bitcoin?
Answer (1 of 7): You should think of this question as "Should I invest all my savings in Bitcoin?", and not "Should I have my savings denominated ...

Convertizo 2 - Convert Units and Currency in Style on the App Store on iTunes
Get Convertizo 2 - Convert Units and Currency in Style on the App Store. See screenshots and ratings, and read customer reviews.

Catholics should not convert Jews, says Vatican, in push against anti-Semitism
... 50 years since the Vatican's repudiation of the concept of Jewish guilt for Jesus' death.  Vatican City: Catholics should not try to convert ...

Opinion: These 5 things made me a happy iPad Pro convert
... like I needed a bigger device. But after three weeks with my iPad Pro, several things have changed my mind, and I’m officially a happy convert ...

Today’s apps gone free: BlockBear, Stock Grader, PDF Smart Convert and more
Block ads and other annoying content, take the emotion out of investing, and convert almost anything into a PDF with todays collection of apps ...

How to convert your old VHS tapes to DVD with this software
Remember those old VHS tapes you have sitting in the corner? Convert them into DVDs with this affordable software so you can easily watch them ...

Resources last updated: 12/26/2015 12:35:38 PM