string-formatting

Calculate a Ratio in C#

我的梦境 提交于 2019-11-27 14:01:38
I thought this would be simple, but searching Google didn't seem to help. I'm basically trying to write a function which will return a ratio as a string (eg 4:3) when supplies with two integers (eg 800 and 600). string GetRatio(Int A, Int B) { // Code I'm looking for return Ratio; } Konrad Rudolph You can simplify fractions by dividing numerator and denominator by their GCD : var gcd = GCD(A, B); return string.Format("{0}:{1}", A / gcd, B / gcd) And a very basic function for calculating the GCD, using the Euclidean algorithm : static int GCD(int a, int b) { return b == 0 ? Math.Abs(a) : GCD(b,

How to deal with “%1” in the argument of QString::arg()?

眉间皱痕 提交于 2019-11-27 13:37:56
问题 Everybody loves QString("Put something here %1 and here %2") .arg(replacement1) .arg(replacement2); but things get itchy as soon as you have the faintest chance that replacement1 actually contains %1 or even %2 anywhere. Then, the second QString::arg() will replace only the re-introduced %1 or both %2 occurrences. Anyway, you won't get the literal "%1" that you probably intended. Is there any standard trick to overcome this? If you need an example to play with, take this #include

Format strings vs concatenation

流过昼夜 提交于 2019-11-27 13:30:00
I see many people using format strings like this: root = "sample" output = "output" path = "{}/{}".format(root, output) Instead of simply concatenating strings like this: path = root + '/' + output Do format strings have better performance or is this just for looks? It's just for the looks. You can see at one glance what the format is. Many of us like readability better than micro-optimization. Let's see what IPython's %timeit says: Python 3.7.2 (default, Jan 3 2019, 02:55:40) IPython 5.8.0 Intel(R) Core(TM) i5-4590T CPU @ 2.00GHz In [1]: %timeit root = "sample"; output = "output"; path = "{}/

Why can “%.10f” % Decimal(u) emit a string with a literal colon?

孤者浪人 提交于 2019-11-27 13:28:21
问题 When formatting a number to be printed, 12 digit numbers are being formatted with a colon immediately after the dot. Why is this happening? This is Python 2.7 on an AIX system. $ uname -a ; /opt/bin/python2.7 AIX myserver 1 6 00F6A5CC4C00 Python 2.7.12 (default, Sep 29 2016, 12:02:17) [C] on aix5 Type "help", "copyright", "credits" or "license" for more information. >>> '{0:.10f}'.format(123456789012) '123456789011.:000000000' >>> from decimal import Decimal >>> u=123456789012 >>> print "%

Return a tuple of arguments to be fed to string.format()

梦想的初衷 提交于 2019-11-27 12:45:22
问题 Currently, I'm trying to get a method in Python to return a list of zero, one, or two strings to plug into a string formatter, and then pass them to the string method. My code looks something like this: class PairEvaluator(HandEvaluator): def returnArbitrary(self): return ('ace', 'king') pe = PairEvaluator() cards = pe.returnArbitrary() print('Two pair, {0}s and {1}s'.format(cards)) When I try to run this code, the compiler gives an IndexError: tuple index out of range. How should I structure

Why does C's printf format string have both %c and %s?

谁都会走 提交于 2019-11-27 11:37:51
Why does C's printf format string have both %c and %s ? I know that %c represents a single character and %s represents a null-terminated string of characters, but wouldn't the string representation alone be enough? Probably to distinguish between null terminated string and a character. If they just had %s , then every single character must also be null terminated. char c = 'a'; In the above case, c must be null terminated. This is my assumption though :) %s prints out chars until it reaches a 0 (or '\0' , same thing). If you just have a char x; , printing it with printf("%s", &x); - you'd have

Converting Float to Dollars and Cents

爱⌒轻易说出口 提交于 2019-11-27 11:33:26
First of all, I have tried this post (among others): Currency formatting in Python . It has no affect on my variable. My best guess is that it is because I am using Python 3 and that was code for Python 2. (Unless I overlooked something, because I am new to Python). I want to convert a float, such as 1234.5, to a String, such as "$1,234.50". How would I go about doing this? And just in case, here is my code which compiled, but did not affect my variable: money = float(1234.5) locale.setlocale(locale.LC_ALL, '') locale.currency(money, grouping=True) Also unsuccessful: money = float(1234.5)

How to format number of decimal places in wpf using style/template?

人走茶凉 提交于 2019-11-27 11:31:22
I am writing a WPF program and I am trying to figure out a way to format data in a TextBox through some repeatable method like a style or template. I have a lot of TextBoxes (95 to be exact) and each one is bound to its own numeric data which can each have their own resolution defined. For example if the data is 99.123 with a resolution of 2 then it should display 99.12. Similarly a data value of 99 and resolution 3 should be displayed as 99.000 (not 99). Is there a way to do this? Edit: I should clarify, there are 95 TextBoxes on the current screen I'm working on, but I want every TextBox

Use StringFormat to add a string to a WPF XAML binding

吃可爱长大的小学妹 提交于 2019-11-27 11:30:55
I have a WPF 4 application that contains a TextBlock which has a one-way binding to an integer value (in this case, a temperature in degrees Celsius). The XAML looks like this: <TextBlock x:Name="textBlockTemperature"><Run Text="{Binding CelsiusTemp, Mode=OneWay}"/></TextBlock> This works fine for displaying the actual temperature value but I'd like to format this value so it includes °C instead of just the number (30°C instead of just 30). I've been reading about StringFormat and I've seen several generic examples like this: // format the bound value as a currency <TextBlock Text="{Binding

String.Format an integer to use a thousands separator without decimal places or leading 0 for small integers

旧街凉风 提交于 2019-11-27 11:25:50
Silly question, I want to format an integer so that it appears with the 1000's separator (,), but also without decimal places and without a leading 0. My attempts so far have been: String.Format("{0} {1}", 5, 5000); // 5 5000 String.Format("{0:n} {1:n}", 5, 5000); // 5.00 5,000.00 String.Format("{0:0,0} {1:0,0}", 5, 5000); // 05 5,000 The output I'm after is: 5 5,000 Is there something obvious that I'm missing? Richard Friend This worked for me. String.Format("{0:#,0} {1:#,0}", 5, 5000); // 5 5,000 ZafarYousafi Try this:- String.Format("{0:n0}",5000) // 5,000 String.Format("{0:n0}",5) // 5