blazorise numeric edit group separating - c#

I'm trying to use blazorise numeric edit group separating attribute. as it is said in it's documentation it should be possible to use it, but as I tried, it's not working.
I would appreciate it if you can help me to use blazorsie components to separate digits define thousands.
for example, when I'm entering value 12345678, it shows 12,345,678
thanks in advance

NumericEdit component does not have a GroupSeparator parameter. The GroupSeparator parameter is on the NumericPicker component:
<NumericPicker #bind-Value="#paymentDocumentCreateCommand.TotalPrice" GroupSeparator="," />
NumericPicker was added in Blazorise version 1.0.0.
NumericPicker vs NumericEdit
Workaround:
You can use the TextEdit component and apply a format when you convert the number to string.
<TextEdit #bind-Text="#TotalCost" />
<p>#_totalCost</p>
#code {
private double _totalCost = 1234567890;
private string TotalCost
{
get
{
return _totalCost.ToString("#,#");
}
set
{
_totalCost = string.IsNullOrEmpty(value) ? 0 : double.Parse(value);
}
}
}
Custom binding formats
You can create a custom input component that does this conversion internally.

Related

DataGridView: How to allow comma and dot as a separators at the same time

I have a DataGridViewTextBoxColumn which is binded to a property. I want to allow user to input numbers no matter what he uses to separate decimals. Also I don't need spaces or commas to separate thousads.
It's simple:
1.908 = 1.908
1,908 = 1.908
And if there is no way to specify format string, can i Replace(",",".") before binding? Or any other way?
Thank you. (Sorry for my English)
Crete another property of String type which will be bounded to that column.
Then set/read value of original property through this
public class YourBindableItem
{
public decimal OriginalValue { get; set; }
public decimal ParsedValue
{
get { return this.OriginalValue.ToString(); }
set
{
string forParse =
value.Replace(",", Globalization.CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator);
decimal temp = 0;
if(decimal.TryParse(forParse,
out temp,
Globalization.CultureInfo.InvariantCulture) == true)
{
this.OriginalValue = temp;
}
//if value wasn't parsed succesfully, original value will be returned
this.RaiseOnPropertyChanged(nameOf(this.ParsedValue));
}
}
}
The DataGridView already formats according to the regional settings for the current user, at least if you data bind to an object data source and the property is numeric (i.e. not a string).
You can test this by opening Region and Language in Windows and switching between e.g. the English (United States) format and Swedish (Sweden). In the former case, the input 2.718 will parse correctly while in the second 2,718 will. You'll have to run without debugging in VS in order to load fresh settings.
(I would not suggest trying to parse both comma and dot as a decimal separator for the same user, if you're thinking of doing that. That's not the expected behavior for most users and it would lead to bugs if the user should happen to use the thousand separator too.)

How do I configure CsvHelper to always quote a specific field in its CSV output?

I am using the CsvHelper package to write my C# models to Csv. I am using fluent class maps (inheriting from CsvClassMap) to define my field to property mappings.
The issue I have is that some of the property values look like dates to excel. For example "2 - 4". I expect the end user to use excel to view these CSV's. I do not want these values to show as dates, so I am looking to have CsvHelper surround this field by quotes. However, I want ONLY this field surrounded by quotes. There are OTHER fields containing data I WANT to be interpreted (e.g. dates). Can I configure my mapping to specify this field should be quoted? I've played with using a type converter, but that's clearly the wrong approach because this is converting the VALUE and not instructing how to format the field.
As of version 12 you can do this:
const int indexToQuote = 4;
csv.Configuration.ShouldQuote = (field, context) =>
context.Record.Count == indexToQuote &&
context.HasHeaderBeenWritten;
So, apparently quoting is not what I needed to do. Excel quite helpfully decides to treat numeric values that look remotely like dates as dates, unless the field begins with a space (which it then will not display). I feel like relying on this is rather hackish, but I'll take it. FWIW, here's the type converter I used:
public class LeadingSpaceTypeConverter : DefaultTypeConverter {
public override string ConvertToString( TypeConverterOptions options, object value ) {
if (value == null ) {
return String.Empty;
}
return String.Concat(" ", value.ToString());
}
}
And the fluent code:
Map( m => m.CompanySize ).TypeConverter<LeadingSpaceTypeConverter>().Index( 4 );

Regex to validate price (type of decimal)

I have looked through stackoverflow trying to find a solution to this but with no luck so hence why I have resulted in asking the question..
I have a field on my form which is price, type of decimal this is optional depending on what they have selected from a dropdown, So I cant use the [Required] attribute.
When the form is submitted if they have chosen a value from the dropdown which requires the user to enter a postage price I then need to check this field to make sure its a valid decimal so to do this I have the following
public static bool IsValid(decimal postagePrice)
{
var regex = new Regex(#"^\d+.\d{0,2}$");
return regex.IsMatch(postagePrice);
}
But it complains and says "Argument type decimal is not assignable to parameter type string" which I understand, I also can't use Decimal.TryParse as that expects a string.
How can I resolve this (I'm not in a position to change the type from decimal to string either)
If all you want is to verify that the value has at most two decimal positions, you could use a modulo:
public static bool IsValid(decimal postagePrice)
{
return postagePrice % 0.01m == 0m;
}
Regular expressions work on strings - it's that simple.
So in one way or another you'll need to covert the decimal to a string before using a regex to validate it.

formatting ajax.actionlink text to display

I have a decimal data type in my model and an annotation to format it so it adds commas after 3 digits:
[DisplayFormat(DataFormatString = "{0:#,###0.00}" + " (USD)")]
public decimal PaidAmount { get; set; }
when I have any DisplayFor(m => m.PaidAmount) the formatting displays correctly (1,200.00 USD). However, in Ajax.ActionLink the first argument takes a string for the text to display so I can't use a lambda expression (m => m.PaidAmount). When I do:
Ajax.ActionLink(Model.PaidAmount.ToString(), //rest of link params)
the formatting doesn't apply to the link text, it shows just a bunch of numbers without commas (1200.00 USD, note there is no comma after the 1)
my guess is that using the capital 'M'odel version of model loses its annotation properties, is there a way to go around this and apply the formatting to the ajax.actionlink?
You can use
#Ajax.ActionLink(string.Format("{0:#,##0.00 USD}", Model.PaidAmount), ...)
Note I think you mean 0:#,##0.00 not 0:#,###0.00 (i.e. 10,200.00 USD, not 1,0200.00 USD)
Data annotations will not work in this case as they are checked in HtmlHelpers and won't affect regular ToString.
You can create an extension method to format your number an call it in your view:
public static class Extensions
{
public static string ToCurrency(this decimal number)
{
return number.ToString("{0:#,###0.00}") + " (USD)";
}
}
In you view: (don't forget to reference the Extensions class either directly in the view or in web.config under views folder)
Ajax.ActionLink(Model.PaidAmount.ToCurrency(), //rest of link params)

Text input validation for textbox in .NET 4.0

I want to validate (in some simple way) if the input text for a textbox is a number, I would use this code: LINK
But here's a problem: I use .NET 4.0 not 4.5, so I don't havePreviewTextInput event.
I could use TextChanged, but now it doesn't have e.Handled validator.
Is there any simple solution for this (I want to avoid tons of code)?
If you have access to the property that will hold the value, you can use a DataAnnotation on the property.
[RegularExpression(Pattern="[0-9]+")]
public string MyProperty { get; set; }
This MSDN article goes abit more in depth about the subject.
It is difficult to determine what a number is and isn't. I would use TryParse. If it fails it's not a number.
string s = "12345";
int number;
bool result = int.TryParse(s, out number);
if(!result)
{
// not a number.
}

Categories