Entanglement allows for the nonlocality of quantum theory, which is the resource behind device-independent quantum information protocols. However, not all entangled quantum states display nonlocality, and a central question is to determine the precise relation between entanglement and nonlocality. Here we present the first general test to decide whether a quantum state is local, and that can be implemented by semidefinite programming. This method can be applied to any given state and for the construction of new examples of states with local hidden-variable models for both projective and general measurements. As applications we provide a lower bound estimate of the fraction of two-qubit local entangled states and present new explicit examples of such states, including those which arise from physical noise models, Bell-diagonal states, and noisy GHZ and W states.