What I'd like is a method to convert a double to a string which rounds using the half-up method. I.e. if the decimal to be rounded is a 5, it always rounds up the previous number. This is the standard method of rounding most people expect in most situations.
I also would like only significant digits to be displayed. That is there should not be any trailing zeroes.
I know one method of doing this is to use the String.format method:
String.format("%.5g%n", 0.912385);
returns:
0.91239
which is great, however it always displays numbers with 5 decimal places even if they are not significant:
String.format("%.5g%n", 0.912300);
returns:
0.91230
Another method is to use the DecimalFormatter:
DecimalFormat df = new DecimalFormat("#.#####");
df.format(0.912385);
returns:
0.91238
However as you can see this uses half-even rounding. That is it will round down if the previous digit is even. What I'd like is this:
0.912385 -> 0.91239
0.912300 -> 0.9123
What is the best way to achieve this in Java?
Suppose I have some output from a command (such as ls -1):
a
b
c
d
e
...
I want to apply a command (say echo) to each one, in turn. E.g.
echo a
echo b
echo c
echo d
echo e
...
What's the easiest way to do that in bash?
I want to double click on a div, but right now it "selects"/highlights a word. How do I make it so that this doesn't happen?
I tried:
$(this).hide().show()
and
$(this).blur()
But it still highlights the word.
So I'm working with authlogic, and I'm trying to duplicate the login functionality to the welcome page, so that you can log in by restful url or by just going to the main page. No, I don't know if we'll keep that feature, but I want to test it out anyway. Here's the error message:
RuntimeError in Welcome#index
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
The code is below. Basically, what's happening is the index view (the first code snippet) is sending the information from the form to the create method of user_sessions controller. At this point, in theory, it create should just pick up, but it doesn't.
PLEASE help. Please. I've been doing this for about 8 hours. I checked Google. I checked IRC. I checked every book I could find. You don't even have to answer, I can to the grunt work if you just point me in the right direction.
<% form_for @user_session, :url => user_sessions_path do |f| %>
<%= f.text_field :email %><br />
<%= f.password_field :password %>
<%= submit_tag 'Login' %>
<% end %>
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
helper_method :current_user_session, :current_user
before_filter :new_session_object
protected
def new_session_object
unless current_user
@user_session = UserSession.new(params[:user_session])
end
end
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
end
Essentially, I just want to change the home button from just hiding my app to killing the activity that is on the screen.
I thought about writing a broadcast receiver that kills the activity, but I'm not sure how to kill the activity from the receiver.
How do I change the functionality of the home button to "finish" the specific activity?
Thanks
I'm trying to bind a param to a join via a named scope., but I'm getting an error.
What is the correct way to do that?
has_one :has_voted, :class_name => 'Vote', :conditions => ['ip = :userIp']
# named scopes
scope :with_vote, lambda {|ip| {
:include => [:has_voted],
# like this ??
:conditions => [:has_voted => {:conditions => {:userIp => ip}} ]
}}
Idea.with_vote(request.ip).all
I believe I need the condition definition in the model for it to appear in the ON clause of a JOIN, rather then in the WHERE one.
Edit I'm trying to get the following query
select Ideas.*, Votes.* from Ideas
left outer join Votes
on Votes.Idea_id = Idea.id AND Votes.ip = {request.ip}
Hello all!
I have an array and variable.
If the variable does not exist in the array to be added, if there is - remove from the array.
Why not work?
$ar = ["a","b","c"];
$vr = "b";
foreach ($ar as $i => $value) {
if ($value == $vr) {
unset ($ar[$i]);
} else {
$ar[] = $vr;
$ar = array_unique($ar);
}
}
Thanks.
I'm trying to replace a bunch of Linux shell scripts with Scala scripts.
One of the remaining challenges is how to scan an entire directory of JARs and place them into the classpath. Currently this is done in the shell script prior to invoking the scala JVM. I'd like to eliminate the shell script completely.
Is there an elegant scala idiom for this?
I have found this other question but in Java it seems hardly worthwhile to mess with it:
http://stackoverflow.com/questions/252893/how-do-you-change-the-classpath-within-java
This is just a hypothetical scenario to illustrate my question. Suppose that there are two threads and one TVar shared between them. In one thread there is an atomically block that reads the TVar and takes 10s to complete. In another thread is an atomically block that modifies the TVar every second. Will the first atomically block ever complete? Surely it will just keep going back to the beginning, because the log is in an inconsistent state?
Suppose I have a span:
<span class="myspan"></span>
.myspan{
background: url(image.png) top left no-repeat;
}
How do I make it so that when people hover my span, it shows "image_hover.png"?
I've just noticed when loading my client's page (http://habbopfm.com/) in Firefox, that it renders what looks like a rectangle is pushed down a few pixels.
It's difficult to describe, but imagine you took a screenshot of the page, opened it in Photoshop, selected a rectangle and moved it down a bit.
IE and Safari don't appear to do this - I'm assuming this is a browser glitch, not a problem with the code. Can anyone confirm what causes this?
Below is a screenshot taken of the issue:
http://habbopfm.com/problem.png
One other thing is that it only does this once the page has finished loading. While it is loading, it looks fine.
I have 2 projects. First project need to include second. When I copy sources from second project to first I need to modify manually import path to R.java file.
The problem is that I need to use SVN external link connection between this 2 projects, that is mean, one project (the bigger one) will take sources from another through svn and should be able to use it without any modifications.
If I would be able to specify R.java file path anywhere in project properties it will resolve my problem by changing manually that path.
Need to specify that package name of these 2 projects are different.
Another solution to resolve this problem also would be accepted.
Thank you.
Hi,
I am trying to filter/search a database with ajax
$.ajax({
type: "POST",
url: "filterSearch.php",
queryString: qry,
success: function(data){
alert( "Data Saved: " + data );
$('#searchResult').html(data); // Fill the search results box
}
});
Now in filterSearch.php i have the following test codes
if(isset($_POST['queryString'])) {
echo "TEST";
}
if($_POST['runquery']==1) {
$sql = "SELECT * FROM fs_vacatures WHERE here-the-like-query?";
$msg = $sql;
echo $msg;
die();
}
die();
But nor TEST or the $sql is return in the alert??
Hi!
I want to add some style to head tag in html page using javascript.
var h = document.getElementsByTagName('head').item(0);
h.innerHTML += '<style>a{font-size:100px;}</style>';
But when I run this code in IE8 I see this error message:
Could not set the innerHTML property. Invalid target element for this operation.
Any ideas?
Hello to all!
I am writing a small Django application and I should be able to create
for each model object its periodical task which will be executed with
a certain interval. I'm use for this a Celery application, but i can't understand one thing:
class ProcessQueryTask(PeriodicTask):
run_every = timedelta(minutes=1)
def run(self, query_task_pk, **kwargs):
logging.info('Process celery task for QueryTask %d' %
query_task_pk)
task = QueryTask.objects.get(pk=query_task_pk)
task.exec_task()
return True
Then i'm do following:
>>> from tasks.tasks import ProcessQueryTask
>>> result1 = ProcessQueryTask.delay(query_task_pk=1)
>>> result2 = ProcessQueryTask.delay(query_task_pk=2)
First call is success, but other periodical calls returning the error
- TypeError: run() takes exactly 2 non-keyword arguments (1 given) in
celeryd server.
So, can i pass own params to PeriodicTask run() ?
Thanks!
Assume I have method
void SomeMethod(Action callback)
This method does some work in background thread and then invokes callback. The question is - how to block current thread until callback is called ?
There is an example
bool finished = false;
SomeMethod(delegate{
finished = true;
});
while(!finished)
Thread.Sleep();
But I'm sure there should be better way
MYMESSAGE = "<div>Hello</div><p></p>Hello"
send_mail("testing",MYMESSAGE,"[email protected]",['[email protected]'],fail_silently=False)
However, this message doesn't get the HTML mime type when it is sent. In my outlook, I see the code...
The title explains it well. I have set up Notepad++ to open the python script in the command prompt when I press F8 but all Swedish characters looks messed up when opening in CMD but perfectly fine in e.g IDLE.
This simple example code:
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
print "åäö"
Looks like this.
As you can see the output of the bath file I use to open Python in cmd below shows the characthers correctly but not the python script above it. How do i fic this?
Suppose this is my URL route:
(r'^test/?$','hello.life.views.test'),
How do I make it so that people can do .json, .xml, and it would pass a variable to my views.test, so that I know to make json or xml?
What I'm trying to do is to print my two-dimensional array but i'm
lost.
The first function is running perfect, the problem is the second or maybe the way I'm
passing it to the "Print" function.
#include <stdio.h>
#include <stdlib.h>
#define ROW 2
#define COL 2
//Memory allocation and values input
void func(int **arr)
{
int i, j;
arr = (int**)calloc(ROW,sizeof(int*));
for(i=0; i < ROW; i++)
arr[i] = (int*)calloc(COL,sizeof(int));
printf("Input: \n");
for(i=0; i<ROW; i++)
for(j=0; j<COL; j++)
scanf_s("%d", &arr[i][j]);
}
//This is where the problem begins or maybe it's in the main
void print(int **arr)
{
int i, j;
for(i=0; i<ROW; i++)
{
for(j=0; j<COL; j++)
printf("%5d", arr[i][j]);
printf("\n");
}
}
void main()
{
int *arr;
func(&arr);
print(&arr); //maybe I'm not passing the arr right ?
}