Convert string with thousands (and decimal) separator into double

前端 未结 2 1264
梦如初夏
梦如初夏 2021-01-06 02:22

User can enter double into textbox. Number might contain thousands separators. I want to validate user input, before inserting entered number into database.

2条回答
  •  清酒与你
    2021-01-06 02:32

    A C solution would be to use setlocale & sscanf:

    const char *oldL = setlocale(LC_NUMERIC, "de_DE.UTF-8");
    double d1 = 1000.43, d2 = 0;
    sscanf("1.000,43", "%'lf", &d2);
    if ( std::abs(d1-d2) < 0.01 )
      printf("OK \n");
    else
      printf("something is wrong! \n");
    setlocale(LC_NUMERIC, oldL);
    

提交回复
热议问题