Я использую следующий алгоритм. Это подобно другим, отправленным здесь, но это - первый пример в C#.
public static class AxisUtil
{
public static float CalcStepSize(float range, float targetSteps)
{
// calculate an initial guess at step size
var tempStep = range/targetSteps;
// get the magnitude of the step size
var mag = (float)Math.Floor(Math.Log10(tempStep));
var magPow = (float)Math.Pow(10, mag);
// calculate most significant digit of the new step size
var magMsd = (int)(tempStep/magPow + 0.5);
// promote the MSD to either 1, 2, or 5
if (magMsd > 5)
magMsd = 10;
else if (magMsd > 2)
magMsd = 5;
else if (magMsd > 1)
magMsd = 2;
return magMsd*magPow;
}
}
Оба равны, и то, что вы используете, полностью ваше предпочтение.
Что касается char
и string
, обратитесь к Zen of Python
, ( PEP 20 или import this
)
Special cases aren't special enough to break the rules.
Строка длины 1 недостаточно особенный, чтобы иметь выделенный тип char
.
Обратите внимание, что вы можете сделать:
>>> print 'Double" quote inside single'
Double" quote inside single
>>> print "Single' quote inside double"
Single' quote inside double
В Python нет ограничения одинарных кавычек для символов и двойных кавычек для строк.
Как вы можете видеть здесь, грамматика явно допускает обе строки.
http://docs.python.org/reference/lexical_analysis.html#string-literals