首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何改进我的函数,如果2位数字,则将浮点数舍入到最接近的10,如果是3位数字,则将其舍入到最接近的100

如何改进我的函数,如果2位数字,则将浮点数舍入到最接近的10,如果是3位数字,则将其舍入到最接近的100
EN

Stack Overflow用户
提问于 2018-12-12 03:27:51
回答 4查看 171关注 0票数 0

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

我的方法并不理想,但它是这样工作的:

  • 获取要舍入的数字,如829
  • 计算位数(3)
  • 使用循环将字符串转换为0(“000”)
  • 在字符串的开头添加1,然后转换为浮点数(1000 )
  • 查找差值(1000-829=171)
  • 获取差值的第一位数字(1),然后将其与浮点数的第一位数字相加,其余数字设置为零("900"),然后转换为数字(900)

这意味着725的y轴最大标签数为800,829为900

我的代码可以工作,但我觉得它只是一堆垃圾,采用了一种粗糙的方法

我必须为大数编码。例如,如果我想要找到的浮点数的最大值是>10000,那么取前两位数,再加上1000。如果>100,000则添加10,000

我如何在这里改进?我有点卡住了,我转换成字符串的想法对吗?!

完整代码如下:

代码语言:javascript
复制
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
}

是基于Matt Timmerman答案的解决方案,但转换为Go中的工作:

代码语言:javascript
复制
func testing(float float64) (YAxisMaximum float64) {
    place := 1.0
    for float >= place*10.0 {
        place *= 10.0
    }
    return math.Ceil(float/place) * place
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-12-12 03:48:06

哇,你的手术太复杂了。如果数字不是很大,我就会这么做。我不知道go,所以我将猜测如何用go语言编写它:

代码语言:javascript
复制
func getMaxYAxisValueForChart(float float64) {

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

Stack Overflow用户

发布于 2018-12-12 03:54:34

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

Or...better取以10为底的对数,得到整数部分,加1,然后返回10的幂:)

代码语言:javascript
复制
    import (
        "fmt"
        "math"
    )

    //func PowerScale(x int) int64{
    //   return int64(math.Pow(10,float64(len((fmt.Sprintf("%d",x))))))
    //}

    func PowerScale(x int) int64 {
     return int64(math.Pow(10,float64(int(math.Log10(float64(x))+1))))
}

    func main() {
        fmt.Println(PowerScale(829))
        fmt.Println(PowerScale(7))
    }
票数 1
EN

Stack Overflow用户

发布于 2018-12-12 04:01:28

您可以使用Math.Log10获取一个数字的大小

代码语言:javascript
复制
int magnitude = (int)Math.Pow(10, (int)Math.Log10(value));

使用它将数字除以,计算上限,然后再将其放大。

没有字符串,没有while循环。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53731049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档