최대 값과 소수 자릿수를 지정할 수있는 RandomDecimal
함수를 만듭니다. 그러나 선택한 마지막 임의의 숫자가 0 일 경우 해당 숫자는 삭제되고 정밀도는 해제됩니다. 이 제로를 잃지 않는 것이 가능한가? 나는 그것을 문자열로 변환 한 다음 십진수로 다시 변환하려고 시도했지만 여전히 삭제됩니다.C#에서 십진수로 후행 0을 버리지 않도록 만드는 방법은 무엇입니까?
public static decimal RandomDecimal(int maxValue, int precision)
{
decimal result = new Random().Next(maxValue);
if (maxValue == 0)
{
return result;
}
int i = 1;
decimal digit = 0;
while (i <= precision)
{
digit = new Random().Next(10);
digit = (digit/(Convert.ToDecimal(Math.Pow(10, i))));
result = result + digit;
i++;
}
// if (digit == 0)
// {
// string resultstring = Convert.ToString(result) + '0';
// result = Convert.ToDecimal(resultstring);
// } This code doesn't do anything because the zero is still dropped.
return result;
}
이러한 종류의 '정밀도'는 문자열로 변환 할 때만 존재합니다. –
@HenkHolterman 사실이 아닙니다. 이는 기존 C# 데이터 유형에도 적용되지만 개념적으로 문자열이 아닌 숫자는 "정밀도"를 가질 수 있습니다. – Servy
저는 십진수에 관해 구체적으로 이야기하고있었습니다. 소수 자릿수는 기록하지 않습니다. –