在String中提取前缀十进制

时间:2016-01-06 06:40:36

标签: javascript html regex decimal

我有以下字符串:

166.313px
.3323px
0.213px
127px

我想从字符串中提取前缀十进制。见下面的输出:

166.313
.3323
0.213
127

我将如何在javascript中执行此操作?

5 个答案:

答案 0 :(得分:3)

只需尝试

var string = '166.313px'; 
alert( string.replace('px', '')); 

答案 1 :(得分:1)

var d = parseFloat("166.313px");
// d = 166.313

答案 2 :(得分:0)

var value = parseFloat(s)将删除最后两个字符(px)。然后,您可以使用 package com.htpj.networkingChatApp; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.EOFException; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.ServerSocket; import java.net.Socket; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingUtilities; public class Server extends JFrame { private static final long serialVersionUID = 1L; private JTextArea displayArea; private JTextField messageEnterField; private ServerSocket server; private Socket connectionSocket; private ObjectInputStream input; private ObjectOutputStream output; private int counter = 1; protected void serverGui() { messageEnterField = new JTextField(); setMessageEnterFieldEditable(false); messageEnterField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { sendDataToClient(e.getActionCommand()); } }); add(messageEnterField, BorderLayout.SOUTH); displayArea = new JTextArea(); add(new JScrollPane(displayArea), BorderLayout.CENTER); displayArea.setEditable(false); // display area is never editable setSize(500, 500); setLocationRelativeTo(null); setTitle("Chat Server"); setVisible(true); } protected void runServer() { try { server = new ServerSocket(2000, 100); while (true) { try { waitForConnection(); // wait for a connection getStreams(); // get input and output streams processConnection(); // process connections } catch (EOFException e) { displayMessage("\n server terminated the connection"); } finally { closeConnection(); ++counter; } } } catch (IOException e) { e.printStackTrace(); } } // ENDOF method runServer private void waitForConnection() { try { displayMessage("Waiting for Connection\n"); connectionSocket = server.accept(); displayMessage("Connection" + counter + "received from: " + connectionSocket.getInetAddress() + "port: " + connectionSocket.getPort()); } catch (IOException e) { e.printStackTrace(); } } // ENDOF method waitForConnection private void getStreams() throws IOException { output = new ObjectOutputStream(connectionSocket.getOutputStream()); output.flush(); // flush output buffer to send header information input = new ObjectInputStream(connectionSocket.getInputStream()); displayMessage("\n Got I/O Streams \n"); } // ENDOF getStreams method private void processConnection() throws IOException { String message = null; sendDataToClient("Connection Successful \n"); setMessageEnterFieldEditable(true); // enable server to type message do { try { message = (String) input.readObject(); displayMessage("\n" + message); } catch (ClassNotFoundException e) { e.printStackTrace(); } } while (!message.equals("CLIENT >> TERMINATE")); } // ENDOF processConnection method private void closeConnection() { displayMessage("\n closing connection \n"); setMessageEnterFieldEditable(false); try { output.close(); input.close(); connectionSocket.close(); } catch (IOException e) { e.printStackTrace(); } } // ENDOF closeConnection method private void sendDataToClient(String messageFromServer) { try { output.writeObject("SERVER>> " + messageFromServer); output.flush(); // flush output to client } catch (IOException e) { e.printStackTrace(); } } // ENDOF sendDataToClient method private void displayMessage(String showMessage) { SwingUtilities.invokeLater(new Runnable() { public void run() { displayArea.append(showMessage); } }); } // ENDOF method displayMessage private void setMessageEnterFieldEditable(final boolean isEditable) { SwingUtilities.invokeLater(new Runnable() { public void run() { messageEnterField.setEditable(isEditable); } }); } } // ENDOF class server

将修剪后的字符串转换为浮点值

答案 3 :(得分:0)

是的,我们可以这样做。

var dec = "0.213px"; 
/// give your string along with numbers which includes decimals also

var prefdec = Number(dec.replace(/[^0-9\.]+/g,""));

alert(prefdec); 

它对我有用。如果有任何挑战,请告诉我

答案 4 :(得分:0)

ORDER BY DESC

使用字符串函数