rounding

Round up a CGFloat in Swift

半城伤御伤魂 提交于 2019-12-28 05:06:39
问题 How can I round up a CGFloat in Swift? I've tried ceil(CDouble(myCGFloat)) but that only works on iPad Air & iPhone 5S. When running on another simulated device I get an error saying 'NSNumber' is not a subtype of 'CGFloat' 回答1: Update : Apple have now defined some CGFloat-specific versions of common functions like ceil : func ceil(x: CGFloat) -> CGFloat ...specifically to cope with the 32/64-bit difference. If you simply use ceil with a CGFloat argument it should now work on all

Round up to the nearest tenth?

懵懂的女人 提交于 2019-12-28 04:18:29
问题 I need to round up to the nearest tenth. What I need is ceil but with precision to the first decimal place. Examples: 10.38 would be 10.4 10.31 would be 10.4 10.4 would be 10.4 So if it is any amount past a full tenth, it should be rounded up. I'm running Ruby 1.8.7. 回答1: This works in general: ceil(number*10)/10 So in Ruby it should be like: (number*10).ceil/10.0 回答2: Ruby's round method can consume precisions: 10.38.round(1) # => 10.4 In this case 1 gets you rounding to the nearest tenth.

Round double to 3 points decimal [duplicate]

試著忘記壹切 提交于 2019-12-28 03:54:06
问题 This question already has answers here : Round a float to a regular grid of predefined points (11 answers) Closed 2 years ago . Currently, I can round a double to an output stream using: output.setf(std::ios::fixed,std::ios::floatfield); output.precision(3); But I'm given a double and I need to make the conversion before I insert it to a vector. So for instance, if the number -0.00078 appears then it equals to 0.000 and I won't need to save it. On the other hand, 1.0009 will become 1.001

In jQuery, what's the best way of formatting a number to 2 decimal places?

孤街浪徒 提交于 2019-12-27 13:39:34
问题 This is what I have right now: $("#number").val(parseFloat($("#number").val()).toFixed(2)); It looks messy to me. I don't think I'm chaining the functions correctly. Do I have to call it for each textbox, or can I create a separate function? 回答1: If you're doing this to several fields, or doing it quite often, then perhaps a plugin is the answer. Here's the beginnings of a jQuery plugin that formats the value of a field to two decimal places. It is triggered by the onchange event of the field

In jQuery, what's the best way of formatting a number to 2 decimal places?

淺唱寂寞╮ 提交于 2019-12-27 13:38:53
问题 This is what I have right now: $("#number").val(parseFloat($("#number").val()).toFixed(2)); It looks messy to me. I don't think I'm chaining the functions correctly. Do I have to call it for each textbox, or can I create a separate function? 回答1: If you're doing this to several fields, or doing it quite often, then perhaps a plugin is the answer. Here's the beginnings of a jQuery plugin that formats the value of a field to two decimal places. It is triggered by the onchange event of the field

Java Round up Any Number

橙三吉。 提交于 2019-12-27 13:04:47
问题 I can't seem to find the answer I'm looking for regarding a simple question: how do I round up any number to the nearest int ? For example, whenever the number is 0.2, 0.7, 0.2222, 0.4324, 0.99999 I would want the outcome to be 1. So far I have int b = (int) Math.ceil(a / 100); It doesn't seem to be doing the job, though. 回答1: Math.ceil() is the correct function to call. I'm guessing a is an int , which would make a / 100 perform integer arithmetic. Try Math.ceil(a / 100.0) instead. int a =

Alternative for Math.round()

偶尔善良 提交于 2019-12-25 19:07:35
问题 In JavaScript when value is 4.3, i want it to round off to 4 and if value is 4.5 or above it rounds off to 5. I want all this without using Math.round() . 回答1: You can do this function RoundNum(number){ var c = number % 1; return number-c+(c/1+1.5>>1)*1 } console.log(RoundNum(2.456)); console.log(RoundNum(102.6)); console.log(RoundNum(203.515)); 回答2: You could also do this: round=num=>(num-~~num>=0.5?1:0)+~~num; Explanation: ~~num is a double bitwise OR, actually it removes everything behind

JQuery round off money / currency to 2 decimals

有些话、适合烂在心里 提交于 2019-12-25 18:31:39
问题 How do I round off a money amount to two decimals using the arithmetic convention that 167,7451 is rounded to 167,75 12,1819784 is rounded to 12,18 Notice I want to do it arithmetically and be returned a float and not a string so simple formatting solutions are not what I'm searching for. So far I found the following: amount = 167.7451; rounded = (amount.toFixed(2)); alert(rounded); however the result of toFixed() is a string, so if I need to do more calculations I need to parse the string to

Using Math.round in javascript adds weird number of 0s at the end [duplicate]

☆樱花仙子☆ 提交于 2019-12-25 18:23:56
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is JavaScript’s Math broken? I'm using Math.round to round the number and for some reason it adds weird number of 0s at the end which should not be there. Here is my code: return Math.round($digit * 1000) / 1000; i want numbers to have 3 decimal points example: Math.round(29.469 * 1000) / 1000 = returns this value: 29.469000000000023 cant seem to figure out why. is there a different way of rounding decimals to

How to set global rounding for DecimalFields in Django project?

烂漫一生 提交于 2019-12-25 17:46:49
问题 I found similar question with answers, but "setting the option in the settings.py" and "setting getcontext() in apps.py" doesn't work. Standard decimal rounding is ROUND_HALF_EVEN, but I need to set ROUND_HALF_UP. And I repeat my question with a quote - "where in the Django application I have to set rounding option, so that it would work globally in the project?" 回答1: For django project can work setting decimal.DefaultContext (py3, py2). This context is most useful in multi-threaded