Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am create windows application using c# 2010, here i using product quantity float datatype, in my c# code how set product quantity.

C#
totqty = Convert.ToInt32(sqldr[0].ToString());



this is my code how to change float. Any one give me idea.

What I have tried:

How to set decimal values for product quantity
Posted
Updated 2-Jun-16 0:11am
Comments
Shahin Khorshidnia 2-Jun-16 5:57am    
Did you tried float.TryPars(...)?
Boopalslm 2-Jun-16 6:00am    
yes i was but error is came

totqty = float.TryParse(sqldr[0].ToString());
Sergey Alexandrovich Kryukov 2-Jun-16 12:25pm    
If you have a string, you should use float.TryParse, nothing else. It may throw exception (exception, not "error") if sqldr is null, or sqldr[0] is null. You can get failure to parse if the string does not correctly represent number.

If you are using a database, you have to use adequate data types. A numeric data should not be represented as string.

—SA
Richard MacCutchan 2-Jun-16 6:00am    
Do not use float variables except in scientific calculations. Product quantities and prices should always use integer or Decimal types.
Boopalslm 2-Jun-16 6:02am    
how to convert my code.

So the value is being returned from an SQL DataReader - which means it is returned in the type it is in the database.
If that's a FLOAT column, then just
C#
float f = (float) (double) sqldr[0];
double d = (double) sqldr[0];
will work,
or even
C#
decimal dc = (decimal) (double) sqldr[0];
 
Share this answer
 
Comments
Boopalslm 2-Jun-16 6:28am    
how to this line convert decimal

decimal qty = Convert.ToSingle(dataGridView1.CurrentRow.Cells[6].Value);
OriginalGriff 2-Jun-16 6:38am    
Why are you converting to a Single (or float) if you want a Decimal value?
Hi,

If your database is returning float values you can use this


C#
decimal totQuantity = 0;
totQuantity = Convert.ToDecimal(sqldr[0]);
txtQuantity.Text = totQuantity.ToString("N2");  // displaying with 2 decimal points


Please let me know if this satisfies your need.

Thanks.
 
Share this answer
 
Comments
Debasis.logica 3-Jun-16 1:39am    
I am not quite sure about the reason for downvoting!!!

Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900