NumberFormatInfo nfi = new NumberFormatInfo(); nfi.NumberDecimalSeparator = ","; double val = double.Parse(text.Text, nfi);
Com este código a conversão utiliza sempre a vírgula como caracter separador.
NumberFormatInfo nfi = new NumberFormatInfo(); nfi.NumberDecimalSeparator = ","; double val = double.Parse(text.Text, nfi);
//num = número a formatar; nDecimals = número de casas decimais
function Precision(num, nDecimals){
var toReturn = "";
num = "" + num;
var pointIdx = num.indexOf('.');
//garantir que é decimal
if(pointIdx == -1){
num += '.';
pointIdx = num.indexOf('.');
}
var limit = pointIdx + nDecimals + 1;
//quando se tem mais casas decimais que aquelas que se pretende
if(num.length > limit){
//arredondar para baixo
if(num[limit] < '5')
{
for(var iter = 0; iter < limit; ++iter)
{
toReturn += num.charAt(iter);
}
return toReturn;
}
//arredondar para cima
else{
for(var iter = 0; iter < pointIdx + nDecimals; ++iter){
toReturn += num.charAt(iter);
}
var lastNum = parseInt(num.charAt(pointIdx + nDecimals)) + parseInt(1);
toReturn += lastNum;
return toReturn;
}
}
//acrescentar 0s à direita se necessário
if(num.length < limit){
while(num.length < limit){
num += '0';
}
}
return num;
}