我有多个看起来像这样的字符串: 550 e,1,550 e,1.550,00 e 我需要将其转换为数字,以便可以比较其值。
我先删除了join()
和from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(0,2, 101)
y1 = 3*np.cos(x*np.pi)
y2 = np.cos(x*np.pi)
fig = plt.figure(figsize=(4, 6))
gs = gridspec.GridSpec(8, 1)
ax1 = plt.subplot(gs[0:6,0])
ax1.plot(x, y1, c='orange')
ax1.set_ylim(-3, 3)
ax1.set_xticks([], [])
ax2 = plt.subplot(gs[6:,0])
ax2.plot(x, y2, c='green')
ax2.set_ylim(-1,1)
ax2.set_xticks([0, 1, 2])
ax2.set_xticklabels([r'0', r'0.5', r'1'])
ax2.set_xlabel(r'$n_g$ (2e)')
plt.tight_layout()
fig.text(-0.025, 0.5, 'Frequency (GHz)', ha='center', va='center', rotation='vertical', size=18)
的货币部分(e),但是如何从中获取数字呢?
答案 0 :(得分:0)
尝试使用类似的内容:
var integer = parseInt(yourString, 10);
十进制数字:
var decimal= parseFloat(yourString);
答案 1 :(得分:-1)
使用Regular Expression
捕获数字,后跟可选的小数。
var regex = /\d+.?\d+/g;
const arrofStrings = ["550 e", "1,550 e", "1.550,00 e"];
const numbers = arrofStrings.map((str) => {
str = str.replace(',', '');
return Number(str.match(regex));
});
console.log(numbers);