Format Numbers to always show only first 4 decimal numbers

Advertisements I’m trying to print my numbers that will show only the first 4 numbers of the decimal and not everything. Unfortunately I didn’t find a solution, I tried some functions attempts but nothing worked out, it always ended up like a number that is .toFixed(4) //Non working function for my purpose… function formatToFourDecimals(num) {… Read More Format Numbers to always show only first 4 decimal numbers

C: why my output isn't rounding up using the round() function in math.h?

Advertisements I’m writing a grayscale function and I’m using round() to round the average of RGB colours but when testing it the round function doesn’t round up answers > x.5 instead it rounds everything down. I have tried changing my int avg to float avg and using the round(), didn’t work. I tried using roundf(),… Read More C: why my output isn't rounding up using the round() function in math.h?

Pandas/Datetime rounding error (Rounding to 10 seconds)

Advertisements I have a problem using the round() funktion on a pandas.Series / DataFrame filled with Timestamps. Code example to round to the nearest 10-second mark: import pandas as pd date = pd.DataFrame([pd.to_datetime(‘2022-03-02 06:46:05’), pd.to_datetime(‘2022-03-02 06:46:15’), pd.to_datetime(‘2022-03-02 06:46:25’), pd.to_datetime(‘2022-03-02 06:46:35’), pd.to_datetime(‘2022-03-02 06:46:45’), pd.to_datetime(‘2022-03-02 06:46:55’), pd.to_datetime(‘2022-03-02 06:47:05’), pd.to_datetime(‘2022-03-02 06:47:15’), pd.to_datetime(‘2022-03-02 06:47:25′)]) date[1] = date[0].round(’10s’) date OUT:… Read More Pandas/Datetime rounding error (Rounding to 10 seconds)

Running this calculation adds an extra decimal point

Advertisements I’m running the following calculation and for some reason, an extra decimal is added to the result: decimal bal = 10.0000 decimal totAmount = 15.0000 decimal payAmount = 3 tempAmount = Math.Ceiling((decimal)(bal / totAmount) * payAmount * 100); tempAmount = tempAmount / 100; // tempAmount results in 2.01 as opposed to 2.00 Am I… Read More Running this calculation adds an extra decimal point

Rounded averages by group that sum to the same as the group total

Advertisements I have data that looks like this: library(dplyr) Data <- tibble( ID = c("Code001", "Code001","Code001","Code002","Code002","Code002","Code002","Code002","Code003","Code003","Code003","Code003"), Value = c(107,107,107,346,346,346,346,346,123,123,123,123)) I need to work out the average value per group per row. However, the value needs to be rounded (so no decimal places) and the group sum needs to equal the group sum of Value. So… Read More Rounded averages by group that sum to the same as the group total