rounding

MySQL round half

天大地大妈咪最大 提交于 2019-12-20 05:12:05
问题 Is it possible, in MySQL, to round half a specific way like PHP would do? PHP_ROUND_HALF_UP PHP_ROUND_HALF_DOWN PHP_ROUND_HALF_EVEN PHP_ROUND_HALF_ODD http://php.net/manual/fr/function.round.php Or are we really limited to only rounding up? 回答1: 1. From Oracle documentation: Based on MySQL official documentation, the rounding function act as the following: For exact-value numbers, ROUND() uses the “round half away from zero” or “round toward nearest” rule: A value with a fractional part of .5

Global decimal rounding options in Django

独自空忆成欢 提交于 2019-12-20 04:54:05
问题 Decimal numbers are by default rounded very unexpectedly, in order to make it work normally, it is needed to use ROUND_HALF_UP option. >>> from decimal import * >>> Decimal("2.5").quantize(Decimal(1)) Decimal('2') >>> getcontext().rounding = ROUND_HALF_UP >>> Decimal("2.5").quantize(Decimal(1)) Decimal('3') >>> Decimal("2.4").quantize(Decimal(1)) Decimal('2') My question is - where in the Django application I have to set rounding option, so that it would work globally in the project? By

Floating point multiplication in java [duplicate]

…衆ロ難τιáo~ 提交于 2019-12-20 04:50:17
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How to round a number to n decimal places in Java When multiplying two numbers in java happens this: double a = 9.495 * 100; Expected result: a = 949.5; But the obtained result is: a = 949.4999999999999 When I try to round number 9.495 in two decimal places the result is 9.49 instead of 9.50 Any ideas how to solve this problem? 回答1: If you want accurate floating point computations, do not use the float or double

DecimalFormat with RoundingMode.HALF_UP

僤鯓⒐⒋嵵緔 提交于 2019-12-20 04:25:28
问题 Having a simple code import java.math.RoundingMode import java.text.DecimalFormat fun main(args: Array<String>) { val f = DecimalFormat("#.##").apply { roundingMode = RoundingMode.HALF_UP } println(f.format(-0.025f)) println(f.format(-0.015f)) println(f.format(-0.005f)) println(f.format(0.005f)) println(f.format(0.015f)) println(f.format(0.025f)) } I got the following output: -0,03 <-- ok -0,01 <-- expecting -0,02 -0 <-- expecting -0,01 0 <-- expecting 0,01 0,01 <--- expecting 0,02 0,03 <--

How to prevent jQuery bootstrap ClockPicker to get minutes that are not divisible by 5

隐身守侯 提交于 2019-12-20 03:55:10
问题 I am using jQuery bootstrap ClockPicker and need to get only times that their minutes are divisible by 5. For example if user selects 13:08, the ClockPicker should select 13:05. Is there a way to override clockpicker to round down selected times minutes? 回答1: I didn't found a way to do this with the API. So I wrote one using afterDone callback. I parse the input's value and make the change to get the new minutes value. var clockpicker = $('.clockpicker').clockpicker({ afterDone: function() {

PHP round the time to the nearest 15 seconds

喜欢而已 提交于 2019-12-20 02:01:21
问题 This is not a duplicate question, but involves a little understanding about time. I need a solution to the following problem I have a number of specifically produced times (based on a date), that need to be rounded to the nearest 15 secs: 60 secs is 1 minute meaning a regular round, floor, ceiling is to the nearest decimal (10/5) which doesn't help me with time. also since I'm dealing with secs, it could be that 59:59 will be rounded up to the nearest hour: e.g. 17:59:59 should be 18:00.

PHP Round function - round up to 2 dp?

拈花ヽ惹草 提交于 2019-12-20 01:43:20
问题 In PHP how would i round up the value 22.04496 so that it becomes 22.05? It seems that round(22.04496,2) = 22.04. Should it not be 22.05?? Thanks in advance 回答1: you can do it using ceil and multiplying and dividing by a power of 10. echo ceil( 1.012345 * 1000)/1000; 1.013 回答2: Do not do multiplication inside a ceil, floor or round function! You'll get floating point errors and it can be extremely unpredictable. To avoid this do: function ceiling($value, $precision = 0) { $offset = 0.5; if (

Python Rounding Down to Custom Step

本秂侑毒 提交于 2019-12-20 01:41:48
问题 We have a partially working code and 2 examples with different types of custom steps. The example 2 (Int) is working, while the example 1 is not, as it is rounding up instead of down. import math def step_size_to_precision(ss): return ss.find('1') - 1 def format_value(val, step_size_str): precision = step_size_to_precision(step_size_str) if precision > 0: return "{:0.0{}f}".format(val, precision) return math.floor(int(val)) ########################### # # example 1 step_size = "0.00000100"

How to round System.Decimal in .Net to a number of significant figures

試著忘記壹切 提交于 2019-12-19 22:08:13
问题 I have a System.Decimal number 0.00123456789 and I wish to round to 3 significant figures. I expect 0.00123 with the behaviour to be a rounding behaviour rather than truncation. Is there a bullet proof way to do this in .Net? 回答1: You can try this... But I don't guarantee anything... Written and tested in 20 minutes and based on Pyrolistical's code from https://stackoverflow.com/a/1581007/613130 There is a big difference in that he uses a long for the shifted variable (because a double has a

Clear trailing 0's on a double?

a 夏天 提交于 2019-12-19 22:01:40
问题 I have a double thats got a value of something like 0.50000 but I just want 0.5 - Is there any way to get rid of those trailing 0's? :) 回答1: standard c format statements. NSLog(@" %.2f", .5000) 回答2: In C, printf("%g", 0.5000); Note: (from GNU libc manual) The %g and %G conversions print the argument in the style of %e or %E (respectively) if the exponent would be less than -4 or greater than or equal to the precision ; otherwise they use the ‘%f’ style. A precision of 0, is taken as 1.