Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 如何改进我的函数,将浮点数四舍五入到最接近的10如果是2位数,100如果是3位数等_Algorithm_Go - Fatal编程技术网

Algorithm 如何改进我的函数,将浮点数四舍五入到最接近的10如果是2位数,100如果是3位数等

Algorithm 如何改进我的函数,将浮点数四舍五入到最接近的10如果是2位数,100如果是3位数等,algorithm,go,Algorithm,Go,我在画条形图,遇到了一个棘手的问题。如何根据给定系列的最大值以编程方式设置y轴标签的最大值。因此,如果你有一个值为7的条,你可能希望y轴上升到10 我的方法并不理想,但工作原理如下: 得到一个整数,比如829 计算位数(3) 使用循环转换为0(“000”)的字符串 将1添加到字符串的开头,然后转换为浮点(1000) 找出差异(1000-829=171) 获取差分的第一位(1),然后将其添加到浮点的第一位,剩余的设置为零(“900”),然后转换为数字(900) 这意味着725将看到y轴最大标签

我在画条形图,遇到了一个棘手的问题。如何根据给定系列的最大值以编程方式设置y轴标签的最大值。因此,如果你有一个值为7的条,你可能希望y轴上升到10

我的方法并不理想,但工作原理如下:

  • 得到一个整数,比如829
  • 计算位数(3)
  • 使用循环转换为0(“000”)的字符串
  • 将1添加到字符串的开头,然后转换为浮点(1000)
  • 找出差异(1000-829=171)
  • 获取差分的第一位(1),然后将其添加到浮点的第一位,剩余的设置为零(“900”),然后转换为数字(900)
这意味着725将看到y轴最大标签号为800,829为900

我的代码是有效的,但我觉得这是一个有黑客手段的垃圾

我必须为大数字编码。例如,如果我要查找的最大值大于10000,则取前两位数字,并将其加上1000。如果>100000,则添加10000

我如何在这里改进?我有点困了,我转换成弦的想法对吗

完整代码如下:

package main

import (
    "fmt"
    "strconv"
)

func main() {

    myFloat := 899175.0

    x := getMaxYAxisValueForChart(myFloat)
    fmt.Println("The number to find the maximum value for is: ", myFloat)
    fmt.Println("This should be the max value for the y axis: ", x)
}

func getMaxYAxisValueForChart(float float64) (YAxisMaximum float64) {
    //Convert to string with no decimals
    floatAsString := fmt.Sprintf("%.f", float)

    //Get length of the string float
    floatAsStringLength := len(floatAsString)

    //For each digit in the string, make a zero-string
    stringPowerTen := "0"
    for i := 1; i < floatAsStringLength; i++ {
        stringPowerTen += "0"
    }

    //Add a 1 to the 0 string to get the difference from the float
    stringPowerTenWithOne := "1" + stringPowerTen

    //Convert the number string to a float
    convertStringPowerTenToFloat := ConvertStringsToFloat(stringPowerTenWithOne)

    //Get the difference from the denominator from the numerator
    difference := convertStringPowerTenToFloat - float

    //We want to isolate the first digit to check how far the float is (100 is far from 1000) and then correct if so
    floatAsStringDifference := fmt.Sprintf("%.f", difference)
    runes := []rune(floatAsStringDifference)
    floatAsStringDifferenceFirstDigit := string(runes[0])

    //For the denominator we want to take away the difference that is rounded to the nearest ten, hundred etc
    runes = []rune(stringPowerTen)
    differenceLastDigitsAsString := ""
    if difference < 10 {
        differenceLastDigitsAsString = "1"
    } else if difference < 30 && difference < 100 {
        differenceLastDigitsAsString = "0"
    } else {
        differenceLastDigitsAsString = floatAsStringDifferenceFirstDigit + string(runes[1:])
    }

    //Convert the number difference string from total to a float
    convertDifferenceStringPowerTenToFloat := ConvertStringsToFloat(differenceLastDigitsAsString)

    YAxisMaximum = convertStringPowerTenToFloat - convertDifferenceStringPowerTenToFloat

    //If float is less than 10,0000
    if float < 10000 && (YAxisMaximum-float >= 500) {
        YAxisMaximum = YAxisMaximum - 500
    }

    if float < 10000 && (YAxisMaximum-float < 500) {
        YAxisMaximum = YAxisMaximum
    }

    //If number bigger than 10,000 then get the nearest 1,000
    if float > 10000 {

        runes = []rune(floatAsString)
        floatAsString = string(runes[0:2])
        runes = []rune(stringPowerTen)
        stringPowerTen = string(runes[2:])
        runes = []rune(stringPowerTenWithOne)
        stringPowerTenWithOne = string(runes[0:(len(stringPowerTenWithOne) - 2)])

        YAxisMaximum = ConvertStringsToFloat(floatAsString+stringPowerTen) + ConvertStringsToFloat(stringPowerTenWithOne)
    }

    if float > 10000 {

        runes = []rune(floatAsString)
        floatAsString = string(runes[0:2])
        runes = []rune(stringPowerTen)
        stringPowerTen = string(runes[:])
        runes = []rune(stringPowerTenWithOne)
        stringPowerTenWithOne = string(runes[0:(len(stringPowerTenWithOne))])

        YAxisMaximum = ConvertStringsToFloat(floatAsString+stringPowerTen) + ConvertStringsToFloat(stringPowerTenWithOne)
    }

    return YAxisMaximum
}

func ConvertStringsToFloat(stringToConvert string) (floatOutput float64) {
    floatOutput, Error := strconv.ParseFloat(stringToConvert, 64)
    if Error != nil {
        fmt.Println(Error)
    }

    return floatOutput
}

哇,这是一个相当复杂的程序。如果数字不是很大,我会这样做。我不知道该怎么写,所以我想猜猜怎么用那种语言写:

func getMaxYAxisValueForChart(float float64) {

    place := 1.0;
    while float >= place*10.0 {
        place *= 10.0;
    }
    return math.Ceil(float/place) * place;
}

哇,这是一个相当复杂的程序。如果数字不是很大,我会这样做。我不知道该怎么写,所以我想猜猜怎么用那种语言写:

func getMaxYAxisValueForChart(float float64) {

    place := 1.0;
    while float >= place*10.0 {
        place *= 10.0;
    }
    return math.Ceil(float/place) * place;
}

取字符串的长度,计算10乘以该长度的幂

或者……最好以10为基数,得到整数部分,加1,然后将其返回到10的幂:)


取字符串的长度,计算10乘以该长度的幂

或者……最好以10为基数,得到整数部分,加1,然后将其返回到10的幂:)


您可以使用Math.Log10获得数字的大小

int magnitude = (int)Math.Pow(10, (int)Math.Log10(value));
使用该值将数字向下划分,计算天花板,然后将其放大


没有字符串,没有while循环

您可以使用Math.Log10获得数字的大小

int magnitude = (int)Math.Pow(10, (int)Math.Log10(value));
使用该值将数字向下划分,计算天花板,然后将其放大


没有字符串,没有while循环

由于829是整数,或可以转换为纯整数解决方案:

func getMaxYAxisValueForChart(int int64) {
    base := 10;
    while int > base*10 {
        base := 10 * base; 
    }
    return int + (base - int) % base;
}

由于829是整数,或可以转换为纯整数解决方案:

func getMaxYAxisValueForChart(int int64) {
    base := 10;
    while int > base*10 {
        base := 10 * base; 
    }
    return int + (base - int) % base;
}


如何改进?您对该代码的具体关注点是什么?正如我所写的,这篇文章似乎比这更适合。我不确定如何在没有硬编码的情况下证明它的未来性。例如,我基本上是在做一个if语句,我说“如果数字是100到1000,那么在数字的第一位加上100,然后把其余的数字设为零”,“如果在1000到10000之间……”如果这有意义的话?伙计,我似乎不能问积极的代表问题。你可能可以使用I=10的
循环解决一些问题;i=i*10
以数量级进行迭代。您可能还想熟悉模运算符
%
,因为使用字符串执行所有这些数字逻辑可能是次优的。如何改进?您对该代码的具体关注点是什么?正如我所写的,这篇文章似乎比这更适合。我不确定如何在没有硬编码的情况下证明它的未来性。例如,我基本上是在做一个if语句,我说“如果数字是100到1000,那么在数字的第一位加上100,然后把其余的数字设为零”,“如果在1000到10000之间……”如果这有意义的话?伙计,我似乎不能问积极的代表问题。你可能可以使用I=10的
循环解决一些问题;i=i*10
以数量级进行迭代。您可能还想熟悉模运算符
%
,因为使用字符串执行所有这些数字逻辑可能是次优的。如果我可以轻松地将您所做的转换为Go,那么是的。。。我弄得一团糟!编辑了我对你所拥有的Go版本的回答,非常感谢:)如果我可以轻松地将你所做的转换为Go,那么是的。。。我弄得一团糟!编辑了我对你所拥有的Go版本的答案,非常感谢:)…是的,有:)…是的,有:)快照。我想知道是否有一些“打印数字,数数数字”比做日志更快。不过,我不确定ICBA是否对其进行了基准测试。这样做更优雅嗯。。。我认为这实际上总是可行的(忽略溢出),但它可以使用注释解释为什么即使在日志向上舍入时结果仍然有效,因为
Log10
返回的浮点结果大于确切值。snap。我想知道是否有一些“打印数字,数数数字”比做日志更快。不过,我不确定ICBA是否对其进行了基准测试。这样做更优雅嗯。。。我认为这实际上总是可行的(忽略溢出),但它可以使用注释解释为什么即使在日志向上舍入时结果仍然有效,因为
Log10
返回的浮点结果大于确切值。