decimal-point

Format a number containing a decimal point with leading zeroes

喜夏-厌秋 提交于 2019-12-04 03:25:00
I want to format a number with a decimal point in it with leading zeros. This >>> '3.3'.zfill(5) 003.3 considers all the digits and even the decimal point. Is there a function in python that considers only the whole part? I only need to format simple numbers with no more than five decimal places. Also, using %5f seems to consider trailing instead of leading zeros. Starting with a string as your example does, you could write a small function such as this to do what you want: def zpad(val, n): bits = val.split('.') return "%s.%s" % (bits[0].zfill(n), bits[1]) >>> zpad('3.3', 5) '00003.3' Is that

do_decimal_point and do_thousands_sep Not Working

落花浮王杯 提交于 2019-12-04 02:31:02
问题 do_decimal_point and do_thousands_sep seem to be completely ignored by my stream. What I want is to do is use a period for my thousands_sep and a comma for my decimal_point in get_money. So I override moneypunct but it is just ignored :( struct punct_facet : public moneypunct<char> { char_type do_decimal_point() const { return ','; } char_type do_thousands_sep() const { return '.'; } }; int main() { istringstream USCurrency("1,234.56 -1,234.56 1.234,56 -1.234,56"); USCurrency.imbue(locale

String.Format - How can I format to x digits (regardless of decimal place)?

流过昼夜 提交于 2019-12-04 00:38:35
I need to format a floating point number to x characters (6 in my case including the decimal point). My output also needs to include the sign of the number So given the inputs, here are the expected outputs 1.23456 => +1.2345 -12.34567 => -12.345 -0.123456 => -0.1234 1234.567 => +1234.5 Please assume there is always a decimal place before the last character. I.e. there will be no 12345.6 number input - the input will always be less than or equal to 9999.9 . I'm thinking this has to be done conditionally. Here's a transparent way to do it without format strings (except for "F"): static void

Sorting 'numbers' with multiple decimal points

前提是你 提交于 2019-12-04 00:35:55
问题 I've got a bunch of "numbers" that have multiple decimal points (so they're really strings). However, I want to sort them as if they were numbers. 1.1.1 10.2.3 2.6.7 21.10.4 3.10.12 4.11.5 4.1.16 6.4.23 I want them to sort by the first set of numbers (before the first decimal point), then by the second set, then by the third (with the possibility of it continuing for a fourth set or more). They should go in this order: 1.1.1 2.6.7 3.10.12 4.1.16 4.11.5 6.4.23 10.2.3 21.10.4 What is the best

Convert decimal number to vector

▼魔方 西西 提交于 2019-12-02 12:21:11
In matlab I want to convert this: 12345.6788993442355456789 into a vector [1 2 3 4 5 6 7 8 8 9 9 3 4 4 2 3 5 5 4 5 6 7 8 9] I have found several solutions to convert a integer into a vector using commands like scanf, num2str,... but those don't fit with non-integers, and some solutions have problems with numbers with a large number of decimal places... That kind of conversion is needed because I want to see and use all of the digits of the number. Andrew Janke What input source are you getting those numbers from? And are all those digits significant? Your example numbers are already beyond the

Read txt file with comma decimal separator in MATLAB [duplicate]

断了今生、忘了曾经 提交于 2019-12-02 04:33:06
问题 This question already has answers here : Matlab: How to read in numbers with a comma as decimal separator? (4 answers) Closed 3 years ago . I have a txt file as such: 1,6 2 6,5 5 ... // ~ 1000 columns 0 1 4 2,5 ... ... // ~1000 rows that is, "," as a decimal separator instead of "." How to read this properly in MATLAB to output as such: 1.6 2 6 5 ... 0 1 4 2.5 ... ... 回答1: There is no easy built-in way of doing this (surprisingly!). You'll want to read in the entire file, then do a string

Using “Decimal” in Python

主宰稳场 提交于 2019-12-02 02:28:29
Can someone please explain what's happening below: (I use Python 3.3) 1. >>> Decimal("0.1") + Decimal("0.1") + Decimal("0.1") - Decimal("0.3") Decimal('0.0') 2. >>> Decimal(0.1) + Decimal(0.1) + Decimal(0.1) - Decimal(0.3) Decimal('2.775557561565156540423631668E-17') 3. >>> Decimal(0.1 + 0.1 + 0.1 - 0.3) Decimal('5.5511151231257827021181583404541015625E-17') I know it has to do with floating point limitation, I'd be glad if someone can explain why What has the " " got to do with the difference between example 1 and 2 above Why does 2 produce a difference answer from 3 given that both have no "

Read txt file with comma decimal separator in MATLAB [duplicate]

天涯浪子 提交于 2019-12-01 22:53:49
This question already has an answer here: Matlab: How to read in numbers with a comma as decimal separator? 4 answers I have a txt file as such: 1,6 2 6,5 5 ... // ~ 1000 columns 0 1 4 2,5 ... ... // ~1000 rows that is, "," as a decimal separator instead of "." How to read this properly in MATLAB to output as such: 1.6 2 6 5 ... 0 1 4 2.5 ... ... There is no easy built-in way of doing this (surprisingly!). You'll want to read in the entire file, then do a string replacement, and then convert the result into numbers. % Read file in as a series of strings fid = fopen('data.txt', 'rb'); strings =

Limiting both the fractional and total number of digits when formatting a float for display

时间秒杀一切 提交于 2019-12-01 17:01:31
I need to print a float value in area of limited width most efficiently. I'm using an NSNumberFormatter , and I set two numbers after the decimal point as the default, so that when I have a number like 234.25 it is printed as is: 234.25 . But when I have 1234.25 I want it to be printed as: 1234.3 , and 11234.25 should be printed 11234 . I need a maximum of two digits after the point, and a maximum of five digits overall if I have digits after the point, but it also should print more than five digits if the integer part has more. I don't see ability to limit the total number of digits in

Sorting 'numbers' with multiple decimal points

折月煮酒 提交于 2019-12-01 03:40:18
I've got a bunch of "numbers" that have multiple decimal points (so they're really strings). However, I want to sort them as if they were numbers. 1.1.1 10.2.3 2.6.7 21.10.4 3.10.12 4.11.5 4.1.16 6.4.23 I want them to sort by the first set of numbers (before the first decimal point), then by the second set, then by the third (with the possibility of it continuing for a fourth set or more). They should go in this order: 1.1.1 2.6.7 3.10.12 4.1.16 4.11.5 6.4.23 10.2.3 21.10.4 What is the best way to do this using JS? I'm thinking I'll probably need to break each number into an array, but there