numbers

How to interpret numbers correctly (hex, oct, dec)

微笑、不失礼 提交于 2019-12-04 19:36:46
I'm trying to write a program that takes input of - hexadecimals, octals, and decimals -, stores them in integer variables, and outputs them along with their conversion to decimal form. For example: User inputs: 0x43, 0123, 65 Program outputs: 0x43 hexadecimal converts to 67 decimal 0123 octal converts to 83 decimal 65 decimal converts to 65 decimal So obviously I need a way to interpret the numbers, but I'm not sure how to go about doing it. I've tried various methods such as reading them into a function and converting them into a string, and vice versa (see here for code examples), but

Rails 3 change default english numbers to arabic numbers

孤街醉人 提交于 2019-12-04 19:33:39
I want to change the default numbers from english to arabic when the user switches to the arabic interface. 13 => ١٣ 89 => ٨٩ What is the best way to tackle this problem? I add in helper module ARABIC_NUMBERS = %w(٠ ١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩) def ta numbers numbers = numbers.to_s if numbers.is_a? Integer results = numbers.chars.map { |char| ARABIC_NUMBERS[char.to_i] }.join end Check this code : https://github.com/gdotdesign/rails-arabic-convert/blob/master/app/helpers/convert_helper.rb . It's a helper to convert a english number to an arabic number. I came up with this quick solution. I added the

Can you manipulate a number within a string with Jquery? [duplicate]

↘锁芯ラ 提交于 2019-12-04 19:26:20
This question already has answers here : How to increase each number in a string? (4 answers) Closed 5 years ago . I have some strings like so: <p>It will vary from 100g to 200g</p> <p>2lbs of meat</p> <p>3 piles of timber</p> etc etc I would like to increment/decrement all numbers in each string. Any pointers? Additional notes: No, the number can be anywhere in the string. @cookie_monster I would like to see if it's possible without the usual operations. A simple solution would be to use a regular expression , for example: "100g".replace(/\d+/g, function(n) { return +n + 1; }); // 101g

Display numbers up to two decimals places without trailing zeros

孤者浪人 提交于 2019-12-04 18:15:28
问题 In my code I will be accepting multiple values, for example: 8.7456 8.7 8 and I need to have them appear as 8.74 8.7 8 i.e. display up to two decimal place. I understand that .toFixed(2) will help me with the first value, but on the 2nd and 3rd value there will be trailing zeroes that I do not want. How to produce my desired results? 回答1: Use Number.toFixed to round the number up to two digits and format as a string. Then use String.replace to chop off trailing zeros: (8.7456).toFixed(2)

How to validate number in perl?

回眸只為那壹抹淺笑 提交于 2019-12-04 18:04:50
I know that there is a library that do that use Scalar::Util qw(looks_like_number); yet I want to do it using perl regular expression. And I want it to work for double numbers not for only integers. so I want something better than this $var =~ /^[+-]?\d+$/ thanks. Constructing a single regular expression to validate a number is really difficult. There simply are too many criteria to consider. Perlfaq4 contains a section "How do I determine whether a scalar is a number/whole/integer/float? The code from that documentation shows the following tests: if (/\D/) {print "has nondigits\n" } if (/^\d+

Generate 4 random numbers that add to a certain value in Javascript

拥有回忆 提交于 2019-12-04 17:39:45
I want a bit of javascript that will allow me to generate 4 random numbers that add up to a certain value e.g. if max = 20 then num1 = 4 num2 = 4 num3 = 7 num4 = 5 or max = 36 then num1 = 12 num2 = 5 num3 = 9 num4 = 10 What I have so far is... var maxNum = 20; var quarter; var lowlimit; var upplimit; var num1 = 1000; var num2 = 1000; var num3 = 1000; var num4 = 1000; var sumnum = num1+num2+num3+num4; quarter = maxNum * 0.25; lowlimit = base - (base * 0.5); upplimit = base + (base * 0.5); if(sumnum != maxNum){ num1 = Math.floor(Math.random()*(upplimit-lowlimit+1)+lowlimit); num2 = Math.floor

C语言程序设计100例之(10):最大公约数

最后都变了- 提交于 2019-12-04 17:10:25
例10 最大公约数 问题描述 有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b。若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c。 输入数据 第一行输入一个n,表示有n组测试数据,接下来的n行,每行输入两个正整数a,b。 输出格式 输出对应的c,每组测试数据占一行。 输入样例 2 6 2 12 4 输出样例 4 8 (1)编程思路。 利用转辗相除法求两个整数的最大公约数。例如,求整数m=48,n=18两个数的最大公约数的方法如左图所示。 具体做法是:,若m%n==0,则n是最大公约数,否则,计算 r=m%n,置m=n,n=r,重复这个过程,直到m%n==0。 将求整数m和n的最大公约数定义为函数 int gcd(int m,intn); 。 在本题中,由于b是a、c的最大公约数,且c!=b,所以对c=2*b、3*b…进行穷举判断即可直到最小的满足条件的c。 (2)源程序。 #include <stdio.h> int gcd(int m, int n) { int r; while(m%n!=0) { r=m%n; m = n; n = r; } return n; } int main() { int t,a,b,c; scanf("%d",&t); while(t--) { scanf("%d%d",&a,&b); c=2*b; while(gcd

How do I format a number with commas?

房东的猫 提交于 2019-12-04 16:28:07
问题 int a = 10000000; a.ToString(); How do I make the output? 10,000,000 回答1: Try N0 for no decimal part: string formatted = a.ToString("N0"); // 10,000,000 回答2: You can also do String.Format: int x = 100000; string y = string.Empty; y = string.Format("{0:#,##0.##}", x); //Will output: 100,000 If you have decimal, the same code will output 2 decimal places: double x = 100000.2333; string y = string.Empty; y = string.Format("{0:#,##0.##}", x); //Will output: 100,000.23 To make comma instead of

Colorize negative/positive numbers (jQuery)

吃可爱长大的小学妹 提交于 2019-12-04 15:12:36
I'd like to color numbers in a table for better readability: green for positive (+00.00); red for negative (-00.00) and; black for default case (no sign) Here ya go: $(document).ready( function() { // the following will select all 'td' elements with class "of_number_to_be_evaluated" // if the TD element has a '-', it will assign a 'red' class, and do the same for green. $("td.of_number_to_be_evaluated:contains('-')").addClass('red'); $("td.of_number_to_be_evaluated:contains('+')").addClass('green'); } Then use CSS to style the input elements: td.red { color: red; } td.green { color: green; }

How does “+var === +var” work internally to verify if var is numeric?

…衆ロ難τιáo~ 提交于 2019-12-04 14:56:36
问题 Seeing this question: Is there a (built-in) way in JavaScript to check if a string is a valid number? and this: jsperf, one of the presented approaches is this (mutatis mutandis): var a = "123" var b = "123b" if ( +a === +a ) // true if ( +b === +b ) // false How does this logic work internally in JavaScript to make this possible? My question is not how to check if a string is a valid number – this is already answered here: Validate decimal numbers in JavaScript - IsNumeric(). I want to