0

I'm trying to record on a SQL Server Database a currency value with commas and dots. In this example: 80,55 in the database stays like this: 8055.

Does anyone knows how can I solve this problem?

In c# i'm doing like this:

public decimal ValorPlanoForm { get; set; }

Then like this:

ValorPlanoForm = message.ValorPlanoForm.ToString("G")

I'm using ASP.NET like this:

<div class="col-xs-6 col-sm-6 col-md-2">
    <div class="form-group">
        <label asp-for="ValorPlanoForm">Valor do plano</label>
        <input asp-for="ValorPlanoForm" type="number" class="form-control">
        <span asp-validation-for="ValorPlanoForm"></span>
    </div>
</div>

Thanks.

Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
Wilton
  • 218
  • 2
  • 11
  • 1
    You want to display the decimal in that format? Or you want your SQL server data to be stored with commas? Because storing in SQL as a decimal with commas/dots is impossible – maccettura Oct 17 '17 at 19:00
  • 2
    You're storing a `decimal`, not a `string`. Decimals are not stored in a particular format. You can *display* it in that format. –  Oct 17 '17 at 19:05
  • Sorry guys my bad. I'm receving it as a decimal but i'm storing it as a string, that's why I need to store with commas. – Wilton Oct 17 '17 at 20:03

2 Answers2

1

You can use use DataType

// Display currency symbol. For country specific currency, set 
// culture using globalization element in web.config. 
// For Great Britain Pound symbol
// <globalization culture="en-gb"/>
[DataType(DataType.Currency)]
public int? Salary { get; set; }

Check this video tutorial in youtube

Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
1

If I were you, I would store the currency data using the Money datatype in SQL Server, and when you want to display the data with commas and dots, you would either format it in your front end application, or using the CONVERT() function in your SELECT query.

But to answer the question you asked, if you really want to store the actual data with commas and dots, you will need to store it as a varchar datatype.

Tab Alleman
  • 31,483
  • 7
  • 36
  • 52
  • In fact, I'm receiving it as a decimal but i'm storing it as a string. But even so it´s storing without the commas. – Wilton Oct 17 '17 at 20:04
  • 1
    Then, during the operation of storing the data from your application, you need to format the value of the variable with the dots and commas you want. – Tab Alleman Oct 17 '17 at 20:16
  • 2
    The `money` datatype is a bad idea (so i've read): https://stackoverflow.com/questions/582797/should-you-choose-the-money-or-decimalx-y-datatypes-in-sql-server – VDWWD Oct 17 '17 at 20:32
  • I find the `money` type useful for some situations, but it's good to be aware of the limitations. – Tab Alleman Oct 18 '17 at 13:38