How to convert milliseconds to seconds with separator?



  • I am building a practice bridging map for me, and need to convert milliseconds to seconds, but with a separator.

    Example:

    Milliseconds Result
    50 0.050
    150 0.150
    4900 4.900
    28769500 28769.500

    The values in the result column is what I want. I want the result to show in the action bar.

    Using datapack and 1.16 Minecraft version.

    Scoreboard objectives:

    Objective Description
    bridgingTickTime bridging time in tick
    bridgingTime bridging time in milliseconds


  • All objectives are internally an integer, meaning you can't natively store floating point on them. Instead, you want to create two objectives to store the integer part and the decimal part and then show them.

    You can also use fake names (invalid Minecraft names using symbols) to store those values instead of having different scoreboards. In my example, I have a score called Time and three variables, $Time, $TimeSI (Time in seconds, integer part) and $TimeSD (Seconds, decimal part). Unfortunately, to make math operations, you need to have a variable with said value, meaning we need one for 1000 and -1 as constants to be able to do said task. Here is how it looks

    After creating the scoreboard, store the constants:

    scoreboard players set $1000 time 1000
    scoreboard players set $-1 time -1
    

    Then, we copy the $Time to $TimeSI and divide by 1000 (The decimal part will disappear due to the data type), followed by $TimeSD getting $TimeSI's value, multiplying by -1000 and subtracting from $Time. Here is the snippet for it:

    execute store result score $TimeSI time run scoreboard players get $Time time
    scoreboard players operation $TimeSI time /= $1000 time
    

    execute store result score $TimeSD time run scoreboard players get $TimeSI time
    scoreboard players operation $TimeSI time *= $1000 time
    scoreboard players operation $TimeSI time *= $-1 time
    scoreboard players operation $TimeSI time += $Time time

    And here is the title command to show the value using the action bar:

     title @a actionbar [{"score":{"name":"$TimeSI", "objective":"time"}},{"text":"."},{"score":{"name":"$TimeSD", "objective":"time"}}]
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2