Variable double receiving comma value in API - . Netcore

Asked

Viewed 363 times

2

Hello,

I created an API that receives two parameters from another project (requirement of college work), is the calculation of the BMI, so it receives weight and height and makes the calculation, until then quiet.

---- API

namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class IMCController : ControllerBase
    {
        [HttpGet]
        public ActionResult<string> Get(double peso, double altura)
        {
            string stg = "";
            //os parametros devem ser passados pela url.
            // EX:  api/ExercicioAPI?peso=75&altura=1.69


            if (peso != 0 && altura != 0)
            {
                double valor = peso / (altura * altura);
                stg = valor.ToString();
            }
            else
            {
                stg = "0";
            }

            string json = "{ imc: " + double.Parse(stg).ToString("0.00") + "}";

            return json;
        }
    }
}

---- In the project controller, I get this:

public double getImc(double peso, double altura)
    {

        double imc = 0;
        using (var client = new WebClient())
        {
            //double teste = 1.59;
            string url = "http://localhost:61822/api/IMC?peso=" + 59 + "&altura=" + altura;
            string json = client.DownloadString(url);

            imc = Convert.ToDouble(new string(json.Where(char.IsDigit).ToArray()));

        }
        return imc;
    }

Every time the url string takes the value with comma ex. 1.59, even coming as string, int, double.

Even when I tested with the variable "test" there, it passed the variable url 1.59

 teste

I tried to pass as string the height in the project API and controller and before the calculation convert to double, but also received with comma, instead of dot.

Any idea what that problem is or how to fix it? I searched and found nothing, I’m using the visual studio in English, I was thinking that it could be something of the system of Brazil use the comma in decimal and be converting in the browser, but I could not find how to fix.

1 answer

1


You need to define in the API which will be the accepted pattern, particularly, would use an integer representing centimeters, and then divide by 100 for the calculation.

When you do [double]. Tostring() it uses the standard operating system culture for representation, i.e., if windows is en-BR, the result will be 1.59 and if en-US will be 1.59.

You can specify which culture to use for the string, including an invariant. Examples:

    altura.ToString("N2"); //Utiliza o padrão do SO
    altura.ToString("N2", CultureInfo.InvariantCulture ); //invariante 1.59
    altura.ToString("N2", CultureInfo.CreateSpecificCulture("pt-BR")); // 1,59
    altura.ToString("N2", CultureInfo.CreateSpecificCulture("en-US")); //1.59

In your controller, if you set the default to en-BR, that is, comma will have to parse this:

    string s = "1,59";
    double d;
    if (double.TryParse(s,NumberStyles.Float,CultureInfo.CreateSpecificCulture("pt-BR"), out d))
    {
        Console.WriteLine("valor: "+d);    
    }

Documentation Tostring Tryparse Cultureinfo

Example code in .netFiddle (the default of . netfiddle is en-US ;) )

When doing Parse of some value, use the Tryparse, here is an answer from the Amazon to a question similar to yours: /a/252560/69359 and in the community there are several other responses demonstrating the use.

I didn’t get into the json question whether it would not evade the question and maybe haven’t seen about in class yet, but it could be a serialized class only, and not write the json "in hand"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.