Keeping Trailing Zeros

Keeping trailing zeros

If this is for printing purposes, sprintf is what you are after:

> sprintf("%.3f", round(5.2,3))
[1] "5.200"

See ?sprintf for formatting details.

How to preserve trailing zeros?

In order to keep the significant trailing zeros use #.

So change

floatfmt=".2g"

to

floatfmt="#.2g"

How to keep trailing 0s when round a number in python?

Short answer, you can't. Unless you modify the underlying object, you cannot change its representation. What you might want is a string formatted to look like this.
Here are two approaches:

my_list = [11, 12.3, 14, 200.33, 49.08, 207.2]
['%.2f'%i for i in my_list]

output:

['11.00', '12.30', '14.00', '200.33', '49.08', '207.20']

f-strings (python >3.6):

[f'{i:.2f}' for i in my_list]

NB. do NOT call a list "list", this is already a python builtin

How to keep the trailing zeros when converting a numeric object to character object in R?

Instead of pasting the strings together have a go with sprintf, you have quite a lot of control with the formatting.

> sprintf("%d.0-%d.0", 71, 100)
[1] "71.0-100.0"


EDIT

The full work would look akin to this.

testdata1 %>% 
group_by(quintiles) %>%
mutate(
Min = min(X),
Max = max(X),
Range = sprintf("%.1f-%.1f", Min, Max)
)

sprintf will sort out the formatting and leading 0s.
Thanks for the correction Roland, not sure what I was doing above

How to keep trailing zeros when dividing a BigDecimal

What you want is formatting as those 0 does not add to value. You can use this and you will get desired output.


BigDecimal endDte = new BigDecimal("2609.8200");
BigDecimal startDte = new BigDecimal("100");

BigDecimal finalPerformance = endDte.divide(startDte);
System.out.printf("%2.6f%n", finalPerformance);

Other option if you always want to divide by 100, you can just shift the decimal. When you do that, the precision remains the same. In that case the new code to try is


BigDecimal endDte = new BigDecimal("2609.8200");
//BigDecimal startDte = new BigDecimal("100");


BigDecimal finalPerformance = endDte.movePointLeft(2);
System.out.println(finalPerformance);

keeping trailing zeros in prettyNum()

format() can easily achieve what you want.

format(c(1, 1.1, 2))
# [1] "1.0" "1.1" "2.0"

format(c(100, 1.1, 2, 2.25))
# [1] "100.00" " 1.10" " 2.00" " 2.25"

If you don't need the leading spaces, just add trim = TRUE.

Keep trailing or leading zeroes on number

No. A number stores no information about the representation it was entered as, or parsed from. It only relates to its mathematical value. Perhaps reconsider using a string after all.


If i had to guess, it would be that much of the confusion comes from the thought, that numbers, and their textual representations would either be the same thing, or at least tightly coupled, with some kind of bidirectional binding between them. This is not the case.

The representations like 0.1 and 0.10, which you enter in code, are only used to generate a number. They are convenient names, for what you intend to produce, not the resulting value. In this case, they are names for the same number. It has a lot of other aliases, like 0.100, 1e-1, or 10e-2. In the actual value, there is no contained information, about what or where it came from. The conversion is a one-way street.

When displaying a number as text, by default (Number.prototype.toString), javascript uses an algorithm to construct one of the possible representations from a number. This can only use what's available, the number value, also meaning it will produce the same results for two same numbers. This implies, that 0.1 and 0.10 will produce the same result.

Concerning the number1 value, javascript uses IEEE754-2019 float642. When source code is being evaluated3, and a number literal is encountered, the engine will convert the mathematical value the literal represents to a 64bit value, according to IEEE754-2019. This means any information about the original representation in code is lost4.

There is another problem, which is somewhat unrelated to the main topic. Javascript used to have an octal notation, with a prefix of "0". This means, that 003 is being parsed as an octal, and would throw in strict-mode. Similarly, 010 === 8 (or an error in strict-mode), see Why JavaScript treats a number as octal if it has a leading zero

In conclusion, when trying to keep information about some representation for a number (including leading or trailing zeroes, whether it was written as decimal, hexadecimal, and so on), a number is not a good choice. For how to achieve some specific representation other than the default, which doesn't need access to the originally entered text (e.g. pad to some amount of digits), there are many other questions/articles, some of which were already linked.


[1]: Javascript also has BigInt, but while it uses a different format, the reasoning is completely analogous.

[2]: This is a simplification. Engines are allowed to use other formats internally (and do, e.g. to save space/time), as long as they are guaranteed to behave like an IEEE754-2019 float64 in any regard, when observed from javascript.

[3]: E.g. V8 would convert to bytecode earlier than evaluation, already exchanging the literal. The only relevant thing is, that the information is lost, before we could do anything with it.

[4]: Javascript gives the ability to operate on code itself (e.g. Function.prototype.toString), which i will not discuss here much. Parsing the code yourself, and storing the representation, is an option, but has nothing to do with how number works (you would be operating on code, a string). Also, i don't immediately see any sane reason to do so, over alternatives.

Keep trailing zeroes in python

When working with money, always represent money using the Decimal class.

http://docs.python.org/2/library/decimal.html

Convert Pandas DF of Floats to Str, Keep trailing zeros

I am not sure if i understood correctly but I think this is a solution:

df = pd.DataFrame({'floats': [0.5, 0.66, 0.]})

df['floats'] = df['floats'].apply(lambda x: "{:.2f}".format(x))
print(df)

>>>
floats
0 0.50
1 0.66
2 0.00


Related Topics



Leave a reply



Submit