humanize

Formatting Large Numbers with .NET

回眸只為那壹抹淺笑 提交于 2019-12-18 03:36:08
问题 I have a requirement to format large numbers like 4,316,000 as "4.3m". How can I do this in C#? 回答1: You can use Log10 to determine the correct break. Something like this could work: double number = 4316000; int mag = (int)(Math.Floor(Math.Log10(number))/3); // Truncates to 6, divides to 2 double divisor = Math.Pow(10, mag*3); double shortNumber = number / divisor; string suffix; switch(mag) { case 0: suffix = string.Empty; break; case 1: suffix = "k"; break; case 2: suffix = "m"; break; case

Natural/Relative days in Python

谁说胖子不能爱 提交于 2019-12-17 09:19:38
问题 I'd like a way to show natural times for dated items in Python. Similar to how Twitter will show a message from "a moment ago", "a few minutes ago", "two hours ago", "three days ago", etc. Django 1.0 has a "humanize" method in django.contrib. I'm not using the Django framework, and even if I were, it's more limited than what I'd like. Please let me (and generations of future searchers) know if there is a good working solution already. Since this is a common enough task, I imagine there must

From: “1 hour ago”, To: timedelta + accuracy

浪子不回头ぞ 提交于 2019-12-10 17:23:14
问题 Is there a function to 'reverse humanize' times? For example, given (strings): '1 minute ago' '7 hours ago' '5 days ago' '2 months ago' Could return (apologies for the pseudo-code): datetime.now() - timedelta (1 minute), accuracy (60 seconds) datetime.now() - timedelta (7 hours), accuracy (1 hour) datetime.now() - timedelta (5 days), accuracy (1 day) datetime.now() - timedelta (2 months), accuracy (1 month) 回答1: I've been using parsedatetime and it's worked rather well for me. The home page

Formatting Large Numbers with .NET

你离开我真会死。 提交于 2019-11-29 02:12:18
I have a requirement to format large numbers like 4,316,000 as "4.3m". How can I do this in C#? You can use Log10 to determine the correct break. Something like this could work: double number = 4316000; int mag = (int)(Math.Floor(Math.Log10(number))/3); // Truncates to 6, divides to 2 double divisor = Math.Pow(10, mag*3); double shortNumber = number / divisor; string suffix; switch(mag) { case 0: suffix = string.Empty; break; case 1: suffix = "k"; break; case 2: suffix = "m"; break; case 3: suffix = "b"; break; } string result = shortNumber.ToString("N1") + suffix; // 4.3m divide the number by

Make big and small numbers human-readable [duplicate]

前提是你 提交于 2019-11-28 09:42:20
This question already has an answer here: Formatting Large Numbers with .NET 5 answers I would like to print my very small numbers in C# in a human friendly way, such as: 30µ for 3E-5 or 456.789n for 0.000000456789 . I know of the Humanize_number () function from BSD in C, but only compatible with bit ints, not floats and doubles. Is there the equivalent in C# that supports those? Also, it should keep a certain amount of precision when displaying numbers, like: 0.003596 should be displayed as 3.596µ , not 3.6µ (or worse, 4µ ). The possible answer here: Formatting Large Numbers with .NET but

How to display “x days ago” type time using Humanize in Django template?

大城市里の小女人 提交于 2019-11-28 06:58:06
When I do this: {% load humanize %} {{ video.pub_date|naturaltime|capfirst }} I get 2 days, 19 hours ago How can I get just 2 days without the hours. Basically if the video was published in less than a day ago then it should say X hours ago, then it should count in days like X days ago, then in weeks. I just don't want 1 hours 5 minutes ago or 2 days 13 minutes ago. Just the first part. I looked at the humanize docs but couldn't find what I needed. Bernhard Vallant Django has a built-in template filter timesince that offers the same output you mentioned above. The following filter just strips

How to capitalize the first character of each word, or the first character of a whole string, with C#?

假装没事ソ 提交于 2019-11-27 07:56:17
I could write my own algorithm to do it, but I feel there should be the equivalent to ruby's humanize in C#. I googled it but only found ways to humanize dates. Examples: A way to turn "Lorem Lipsum Et" into "Lorem lipsum et" A way to turn "Lorem lipsum et" into "Lorem Lipsum Et" Spoike As discussed in the comments of @miguel's answer , you can use TextInfo.ToTitleCase which has been available since .NET 1.1. Here is some code corresponding to your example: string lipsum1 = "Lorem lipsum et"; // Creates a TextInfo based on the "en-US" culture. TextInfo textInfo = new CultureInfo("en-US",false)

Natural/Relative days in Python

梦想与她 提交于 2019-11-27 07:25:09
I'd like a way to show natural times for dated items in Python. Similar to how Twitter will show a message from "a moment ago", "a few minutes ago", "two hours ago", "three days ago", etc. Django 1.0 has a "humanize" method in django.contrib. I'm not using the Django framework, and even if I were, it's more limited than what I'd like. Please let me (and generations of future searchers) know if there is a good working solution already. Since this is a common enough task, I imagine there must be something. While not useful to you at this very moment, it may be so for future searchers: The babel

Make big and small numbers human-readable [duplicate]

那年仲夏 提交于 2019-11-27 03:06:51
问题 This question already has an answer here: Formatting Large Numbers with .NET 5 answers I would like to print my very small numbers in C# in a human friendly way, such as: 30µ for 3E-5 or 456.789n for 0.000000456789 . I know of the Humanize_number() function from BSD in C, but only compatible with bit ints, not floats and doubles. Is there the equivalent in C# that supports those? Also, it should keep a certain amount of precision when displaying numbers, like: 0.003596 should be displayed as

How to display “x days ago” type time using Humanize in Django template?

心已入冬 提交于 2019-11-27 01:38:14
问题 When I do this: {% load humanize %} {{ video.pub_date|naturaltime|capfirst }} I get 2 days, 19 hours ago How can I get just 2 days without the hours. Basically if the video was published in less than a day ago then it should say X hours ago, then it should count in days like X days ago, then in weeks. I just don't want 1 hours 5 minutes ago or 2 days 13 minutes ago. Just the first part. I looked at the humanize docs but couldn't find what I needed. 回答1: Django has a built-in template filter