Save price as decimal or string? [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an ASP.NET web project. I have a "price" field in my database, which is defined as decimal.
When the user inserts price in a textbox, the string from textbox is saved in the "price" field (I force the user to input only digits).
All data is presented in a ListView. The issue is when the user doesn't provide any value (empty textbox), I get an exception.
I can understand why this happens: because it can't cast an empty string to the decimal type. If the user didn't provide any value, I want to present an empty field in the ListView. How can I achieve that?

I suggest to have a default value 0.00 than an empty or null value. If user did not provide any value, validate it and convert the value of the textbox to 0.00.
If you don't want this suggestion check your DB then allow null the decimal field.

This is your own logical validation. However you can set your price field to nullable or check your textbox if has no value then set your price to 0.00

Related

c# adding money together removes the 0 at the end [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
In c# when adding two decimals the program will automatically get rid of the number 0 after the decimal places.
For example adding 0.50 to 1.20 will produce the answer of 1.7 and this is annoying because i need to display this answer in terms of money.
Is there a way to prevent this?
If you want to display your Decimal with two decimal places, please use :
myDecimal.ToString("N2");
You may want to take a look at Standard Numeric Format Strings for more information.
decimal d = 0.50m;
decimal d1 = 1.20m;
Console.Write(d+d1);
Please find this Post
I'm not sure about if you mean this, but you can try the toString() method in currency format this way:
double number = 1.2;
string numberCurrency = number.ToString("C");
Console.WriteLine(numberCurrency); //this prints "1.20"
I recommend you to read this https://msdn.microsoft.com/es-es/library/kfsatb94(v=vs.110).aspx

What is the purpose of CultureInfo.CurrentCulture? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
what is CultureInfo.CurrentCulture in c# , actually unable to find the answer , why we use it , for what purpose , please explain in easy words
Please Explain in easy words ! Why we use this . On the other hand , if i donot use this , results are still same . So why what is the purpose of this ?
You can think of CultureInfo as a collection of settings for a particular geographical region.
A CultureInfo object specifies how a piece of information is rendered for a particular culture. It contains settings for writing directions and other related settings, dates formatting, calender, currency etc.
With CultureInfo.CurrentCulture the culture context for the current thread is given. This is used by functions like ToString() to define the string format of a DateTime value, when no explict culture is provided as a function parameter.
CultureInfo contains information on how to deal with data representation in different countries.
As for example date format (in my country it's DD/MM/YYYY, in the US it's MM-DD-YYYY) or floating point number representation (3,14 vs 3.14).

String to decimal [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The application that I've made I've got a text box called service cost. This allows the user to enter the cost of the service they have provided and this is decimal. I'm trying to get this service cost displayed in a DGV, I've got everything else working apart from this.
currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost);
The above is the code that I currently have, is there something wrong that I've done here?
From the question it is clear that txtServiceCost is the TextBox, and Convert.ToDecimal() expects a string as input so you should use txtServiceCost.Text instead for txtServiceCost. Since txtServiceCost is a control where as txtServiceCost.Text is a string
currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost.Text);
But i would like to suggest you to use decimal.TryParse
decimal userInput;
if (!decimal.TryParse(txtServiceCost.Text, out userInput))
{
// Throw some warning here that invalid input
}
currentComputer.ServiceCost = userInput;

ListBox get value from index [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a program where you insert some numbers in a listbox and then a button click should get the values from the listbox and check whether they are positive or negative and display each other's count in a textbox.
I tried getting the value by: string x = listBox1.Items[index].Value; but it doesn't seem to work.
If you add the items to the list box as such:
listBox1.Items.Add(textBox1.Text);
Then you can retrieve the item from given index as follows:
string x = listBox1.Items[index];
The indexer is returning the value, which in that case is a string. Probably you might need to cast it to string, because the indexer actually returns object - see here: ListBox.ObjectCollection.Item Property :
string x = (string)listBox1.Items[index];
You can also try
string s = (string)listbox1.Items.GetItemAt(index);
You have to cast it to string because it's returning an object

Recognize the type of a value saved in the Database as a String [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a problem that I'm not able to solve.
I'm getting a value from a Database. This value in the database is saved as a nvarchar.
I need to understand the type of the value.
For example:
12.12.2012 -> DateTime
15:00 -> Time
67.45 -> Double
For undesrtand the type I use the TryParse method.
Example for understand if it is a DateTime I execute this code:
DateTime valueConverted;
if(DateTime.TryParse(input.ToString(), out valueConverted))
{
// IS DATETIME
}
The problem is that if the value got from the DataBase is:
0900
009000
I would like the recognize it as a String. The problem is that the conversion to a double has success and then the value is converted to a double with value = 900.
How can I recognize that the value like 0900, 00060 are not double but String?
The simplest would probably be to match against ^0[0-9]+$.
Realize though this is a non-trivial problem and you're bound to find edge cases you didn't expect. I recommend changing the data model if you are at all able, and either store different types in different columns or have a separate column that denotes the data type stored. Storing everything as a string is generally a Bad Idea.
This seems massively oversimplified, but based on your description it will do the trick:
if (!string.IsNullOrEmpty(input) &&
input[0] == '0' &&
input.All(c => char.IsDigit(c)))
{
// We have a string.
}
you can used regex-match
create all type of datatype format string and match with regex.
http://www.dotnetperls.com/regex-match
if (input.StartsWith("0"))
{
// string
}
else
{
// double
var number = double.Parse(input);
}

Categories

Resources