Disable auto-formatting of number in an nvarchar()

Disable auto-formatting of number in an nvarchar()

Vincent.PVincent.P Posts: 35Questions: 6Answers: 0

Hi,

As we are working in a project that requires the user to save a string as the following format "100.000", it seems that for some weird reason, the editor is just removing the dot without us asking for it.

We are using the latest version available of the .Net libraries.

Is there a way to prevent that behavior?

This is really annoying as it is not the default behavior that should happen if we don't ask for it.

Best regards,
Vincent.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,525Questions: 1Answers: 10,473 Site admin

    the editor is just removing the dot without us asking for it.

    Is it saving 100000 as the value? Or is it folding the number down to be an integer (100)? By guess is the latter, and that would be caused by the conversion from HTTP string data to a number.

    I wonder if we should be adding an extra condition in there whereby numbers with a decimal place and a trailing zero should not be cast as a number. My concern there is that some other developers would be expecting numeric data to be a number!

    Possibly the best thing here is to use a set formatter to convert the number into a string with the three decimal places...

    Allan

  • Vincent.PVincent.P Posts: 35Questions: 6Answers: 0
    edited July 2021

    Hi Allan, it is saving it as 100000. If we add anything else as a character like 100.000e, it is fine, if we save it with 100,000 it is also fine. It only removes the dot. It also removes the dots if we send 10.10.10, it also becomes 101010.

    Unfortunately, we can't really use a formatter as the values could be without any dots. Basically, the values we have can be something like:

    100, 100.000, 100.001, 999.050.

    100 and 100.000 should be different values

  • allanallan Posts: 63,525Questions: 1Answers: 10,473 Site admin

    it is saving it as 100000

    Wow, I have no idea what is causing that I'm afraid! In the Network Inspector in your browser, is it correctly sending 100.000 in the Ajax request data?

    Can you show me the server-side code you are using please?

    Thanks,
    Allan

  • Vincent.PVincent.P Posts: 35Questions: 6Answers: 0
    edited July 2021

    Hi Allan,

    Yes it is correctly sending 100.000. Another strange behavior is that it is working fine on one computer but not on the computer from our other developer. So apparently there is something happening based on the configuration of the OS language used? (I suspect it could happen because of that)

    And here the server-side code:

    using (var db = new Database("sqlserver", Config.ConnectionString))
    {
        var editor = new Editor(db, "tablesector")
            .Field(new Field("tablesector.id")
                .Set(Field.SetType.Create)
            )
            .Field(new Field("tablesector.official_pc", typeof(String)) // This field has issues
                // .Validator(Validation.None())
            )
            .Field(new Field("tablesector.custom_pc") // This field has same issue as well
            );
                    
        var result = editor.TryCatch(true)
            .Debug(true)
            .Process(Request.Form)
            .Data();
        
        var requestType = Editor.Action(HttpContext.Request);
        
        var json = JsonSerializer.Serialize(result, new JsonSerializerOptions{ IncludeFields = true });
        return new ContentResult { Content = json, ContentType = "application/json" };
    }
    
  • allanallan Posts: 63,525Questions: 1Answers: 10,473 Site admin
    Answer ✓

    I suspect what is happening is that the "culture" is set to be for a country / language which uses . as the thousands separator on the computer which is removing the ., but not for the computer where it works as expected (this wikipedia map is interesting on this topic).

    Does that sound like it might be the case?

    I still suspect the casting I linked to before. Could you change .Process(Request.Form) to be:

    .Process(Request.Form, "en-US")
    

    and see if that addresses the issue please?

    Thanks,
    Allan

  • Vincent.PVincent.P Posts: 35Questions: 6Answers: 0

    Hi Allan,

    Sorry for the late answer, we just tested it and it seems to properly solve that specific issue. Thanks a lot :smile:

  • allanallan Posts: 63,525Questions: 1Answers: 10,473 Site admin

    No problem - good to hear that did the job!

    Allan

Sign In or Register to comment.